@typescript-eslint/method-signature-style

Enforce one way to write a method in an interface or type literal

fixable

Also called typescript-eslint/method-signature-style, typescript/method-signature-style

An interface can declare a function member in two ways: as a method, save(user: User): void, or as a property with a function type, save: (user: User) => void. They look the same to a caller, but TypeScript checks them differently. With strictFunctionTypes, TypeScript checks a property's parameters strictly. It doesn't do that for a method's parameters, so you can assign a function that only accepts a narrower type, and it fails when it's called with the wider one.

By default the rule asks for the property form.

--fix rewrites the signature. Overloads of one method become an intersection of function types. These are reported but not fixed:

  • a method that returns this
  • a method inside a namespace or declare module block
  • with "method", a readonly property, because a method can't be readonly. The report offers the change as a suggestion.

Configure#

Off by default. To turn it on:

bunfig.toml
[lint.rules]
"@typescript-eslint/method-signature-style" = "error" # or "warn"

Every option, set to its default:

bunfig.toml
[lint.rules]
# "property" or "method"
"@typescript-eslint/method-signature-style" = ["error", "property"]

Examples#

example.ts1 problem
interface Store {
  save(user: string): void;
}
example.tsNo problems
interface Store {
  save: (user: string) => void;
  get size(): number;
}

Options#

bunfig.toml
[lint.rules]
"@typescript-eslint/method-signature-style" = ["error", "method"]
OptionWhat it does
"property" (default)Report save(user: string): void.
"method"Report save: (user: string) => void.
example.tswith "method"1 problem
type Store = {
  save: (user: string) => void;
};
example.tswith "method"No problems
type Store = {
  save(user: string): void;
};

When not to use it#

Without strictFunctionTypes, which is part of strict, TypeScript checks both forms the same way and the rule is only about style.