TypeScript Reference ยท Literal and Object Types
TypeScript call signatures
Reference for call signatures in TypeScript: describing callable objects with properties, overloads in one type, generics, and how they differ from function types.
A call signature is an entry in an object type that says "you can call a value of this type". It looks like a function parameter list followed by a return type, written inside braces: { (x: number): string }. The compact arrow form (x: number) => string describes the same thing; the call-signature form is needed when the function also has properties, or when you want several overloads in one type.
In JavaScript functions are objects, so a function can carry extra properties such as a description or a cache. A call signature plus property members captures such hybrids exactly.
type Greeter = { (name: string): string; language: string; };
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.
Syntax
type Fn = { (a: A, b?: B): R }; interface WithProps { (x: number): string; extra: boolean } interface Overloaded { (x: string): string; (x: number): number } interface Generic { <T>(x: T): T } type Arrow = (a: A) => R;
| Part | Description |
|---|---|
| Parameter list | Same rules as functions: optional ?, defaults are not allowed in types, rest ...args. |
| Return type | Required in a call signature; use void or unknown where appropriate. |
| Type parameters | <T> before the parameter list makes the signature generic. |
| Other members | Properties and methods may sit beside the signature. |
Edge cases: in an arrow-style function type, the return type is introduced with =>; in an object type with a call signature it is introduced with :. A value is assignable to an overloaded type only if it is compatible with every overload.
Usage notes
- TypeScript lets you assign properties onto a function declaration or a
constarrow function right after it (fn.version = 1) and infers the combined type. In other cases use an assertion, asmakeCounterdoes. - Overloads in a call-signature type are for callers; the implementation signature of an overloaded function declaration is not visible to them.
- Call signatures and construct signatures can live in one type: some library values are both callable and constructible.
- The contextual type of an arrow function assigned to a call-signature type comes from the signature, so parameters need no annotations (see
id). Functionis a loose catch-all type; a call signature is almost always the better annotation.- Method syntax in an interface (
run(x: number): void) is shorthand for a property whose type has a call signature. - Parameter bivariance differs: method-style declarations are checked bivariantly, call signatures in function-typed properties are checked strictly under
strictFunctionTypes. thiscan be declared as a first fake parameter:(this: Window, e: Event): void.