Skip to content

TypeScript Reference ยท Literal and Object Types

TypeScript method signatures

Learn TypeScript method signatures in interfaces and type literals, how they differ from function-typed properties, and why strictFunctionTypes treats them differently.

A method signature describes a function-valued member of an object type using the shorthand name(params): Return. It looks like a method declaration in a class, but without a body. The alternative is a property signature whose type happens to be a function type: name: (params) => Return.

At the call site the two look identical, and for most code the choice is stylistic. There is one real difference in the type system: parameters of method signatures are compared bivariantly, while parameters of function-typed properties are compared contravariantly under strictFunctionTypes. That makes method syntax more lenient when you assign one object to another.

TypeScript
interface Counter {
  increment(by: number): number; // method signature
  reset: () => void;             // property signature with a function type
}

Try it

TypeScript Two ways to declare a function member

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.

TypeScript Bivariance of methods versus properties

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

TypeScript
interface Example {
  name(param: Type): ReturnType;       // method signature
  name?(param: Type): ReturnType;      // optional method
  readonly name: (param: Type) => R;   // property signature (can be readonly)
  name<T>(value: T): T;                // generic method
  name(this: Example): void;           // method with a this parameter
}
Part Description
name The member name. Can be an identifier, a string literal, a number or a computed name such as [Symbol.iterator].
param: Type Parameter list, same rules as any function type: optional (?), default-free, rest allowed.
ReturnType Return type. If omitted in a declaration, it is implicitly any, which is an error under noImplicitAny.

Edge cases: a method signature cannot be readonly; only property signatures can be marked that way. Repeating the same method name in one interface creates overloads, whereas repeating a property name is a duplicate identifier error.

Usage notes

  • Variance: parameters of method signatures are bivariant even with strict on. Function-typed properties and standalone function types are checked strictly (contravariant parameters). This is why Array<T> and many DOM interfaces use method syntax: it keeps Array<Dog> assignable to Array<Animal>.
  • Overloads: only method signatures can be repeated to declare overloads. For a property you would need an intersection of function types or a type with several call signatures.
  • readonly: write readonly handler: () => void if the member must not be reassigned. Methods declared with shorthand can still be overwritten unless the whole object is readonly.
  • this: methods in an object type conventionally refer to the receiver. See this parameters for declaring it explicitly.
  • Declaration only: interface and type literals contain signatures without bodies. Class methods and object literal methods have bodies, and the compiler infers a compatible method signature from them.
  • Style rule of thumb: prefer the property form when you want strict checking of callbacks you store (for example event handlers); prefer method form for natural "behavior" members.
  • Lint rule: the @typescript-eslint/method-signature-style rule can enforce either style across a project.