← All languages
TypeScript construct of the day
Random

call signatures

Describe callable objects with properties using an interface.

Description

A call signature in an interface or type literal describes how an object can be called as a function while also having properties. This allows typing objects that are both callable and have attached metadata or configuration.

Call signatures use the syntax (params): ReturnType without the function keyword. They can be overloaded by including multiple call signatures in the same interface. The interface can also include regular properties alongside call signatures.

This pattern is common in JavaScript libraries where functions have additional properties attached, such as Express middleware, jQuery, or assertion libraries where the function itself is callable but also has utility methods.

Example

interface Formatter {
  (input: string): string;
  locale: string;
  version: number;
}

const fmt: Formatter = Object.assign(
  (input: string) => input.trim(),
  { locale: 'en-US', version: 1 }
);

fmt('  hello  '); // 'hello'
fmt.locale;       // 'en-US'

Reference