Skip to content
← Back to rules

typescript/method-signature-style Style

🚧 An auto-fix is planned for this rule, but not implemented at this time.

What it does

Enforce using a particular method signature syntax.

Why is this bad?

TypeScript provides two ways to define an object/interface function property:

ts
interface Example {
  // method shorthand syntax
  func(arg: string): number;

  // regular property with function type
  func: (arg: string) => number;
}

The two are very similar; most of the time it doesn't matter which one you use. However, when TypeScript's strictFunctionTypes option is enabled, there is an important difference: methods are always bivariant in their arguments, while function properties are contravariant. This means that switching from method syntax to property syntax (or vice versa) can cause TypeScript to report new type errors or stop reporting existing ones.

A good practice is to use the TypeScript's strict option (which implies strictFunctionTypes) which enables correct typechecking for function properties only (method signatures get old behavior).

TypeScript FAQ:

A method and a function property of the same type behave differently. Methods are always bivariant in their argument, while function properties are contravariant in their argument under strictFunctionTypes.

See the reasoning behind that in the TypeScript PR for the compiler option.

Examples

Examples of incorrect code for this rule with property option:

ts
interface T1 {
  func(arg: string): number;
}
type T2 = {
  func(arg: boolean): void;
};
interface T3 {
  func(arg: number): void;
  func(arg: string): void;
  func(arg: boolean): void;
}

Examples of correct code for this rule with property option:

ts
interface T1 {
  func: (arg: string) => number;
}
type T2 = {
  func: (arg: boolean) => void;
};
// this is equivalent to the overload
interface T3 {
  func: ((arg: number) => void) & ((arg: string) => void) & ((arg: boolean) => void);
}

Examples of incorrect code for this rule with method option:

ts
interface T1 {
  func: (arg: string) => number;
}
type T2 = {
  func: (arg: boolean) => void;
};

Examples of correct code for this rule with method option:

ts
interface T1 {
  func(arg: string): number;
}
type T2 = {
  func(arg: boolean): void;
};

Configuration

This rule accepts one of the following string values:

"property"

Enforce using property signature for functions. Use this to enforce maximum correctness together with TypeScript's strict mode.

"method"

Enforce using method signature for functions. Use this if you aren't using TypeScript's strict mode and prefer this style.

How to use

To enable this rule using the config file or in the CLI, you can use:

json
{
  "rules": {
    "typescript/method-signature-style": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  rules: {
    "typescript/method-signature-style": "error",
  },
});
bash
oxlint --deny typescript/method-signature-style

Version

This rule was added in vnext.

References