@typescript-eslint/method-signature-style
Enforce one way to write a method in an interface or type literal
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
namespaceordeclare moduleblock - with
"method", areadonlyproperty, because a method can't bereadonly. The report offers the change as a suggestion.
Configure#
Off by default. To turn it on:
[lint.rules]
"@typescript-eslint/method-signature-style" = "error" # or "warn"Every option, set to its default:
[lint.rules]
# "property" or "method"
"@typescript-eslint/method-signature-style" = ["error", "property"]Examples#
interface Store {
save(user: string): void;Shorthand method signature is forbidden. Use a function property instead.
}interface Store {
save: (user: string) => void;
get size(): number;
}Options#
[lint.rules]
"@typescript-eslint/method-signature-style" = ["error", "method"]| Option | What it does |
|---|---|
"property" (default) | Report save(user: string): void. |
"method" | Report save: (user: string) => void. |
"method"1 problemtype Store = {
save: (user: string) => void;Function property signature is forbidden. Use a method shorthand instead.
};"method"No problemstype 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.