no-method-signature
A member written as a method with parameters in an interface or a type literal: handle(animal: Animal): string. Write it as a property with a function type, readonly handle: (animal: Animal) => string. On by default, as an error.
Why: strictFunctionTypes checks a function's parameters contravariantly, so a function that only takes a Dog can't be used where one taking any Animal is expected. Members written as methods are exempt: TypeScript checks their parameters in both directions (bivariance). So an object whose handle needs a Dog is accepted as a Handler, and the first cat it is given crashes it, in code that compiled with strict and no cast. Written as a property, the same assignment is refused.
✅ Accepted: a property with a function type
✓ Accepted0 diagnostics
ts
type Animal = { readonly kind: string };
export interface Handler {
readonly handle: (animal: Animal) => string;
}
export const describer: Handler = { handle: (animal) => `a ${animal.kind}` };❌ Rejected: a method signature
✕ Rejectedno-method-signature
ts
type Animal = { readonly kind: string };
type Dog = { readonly kind: string; readonly bark: () => string };
export interface Handler {
handle(animal: Animal): string;
}
const dogOnly = { handle: (dog: Dog): string => dog.bark() };
// Accepted, because `handle` is a method; `handler.handle({ kind: "cat" })` then throws
// TypeError: dog.bark is not a function.
export const handler: Handler = dogOnly;The same goes for a type literal (type Sink = { accept(value: string): void }), an inline parameter type (listener: { handle(event: Event): void }) and a type argument. An optional method (seek?(offset: number): void) becomes readonly seek?: (offset: number) => void.
What the property form closes, and what it leaves
Checked with tsc 7 (strict, noUncheckedIndexedAccess, exactOptionalPropertyTypes): an implementation whose parameter is narrower ((dog: Dog) => ...) assigned to the member, written each way.
| Implementation | Member is a method | Member is a property with a function type |
|---|---|---|
an object literal with an arrow function ({ handle: (dog: Dog) => ... }) | accepted | refused |
an object literal with a method ({ handle(dog: Dog) { ... } }) | accepted | refused |
| an instance of a class with an ordinary method | accepted | refused |
class X implements Handler with an ordinary method | accepted | refused |
a call signature (interface F { (animal: Animal): string }) | refused (always strict) |
So the form of the target member decides, whatever the implementation looks like: a class with ordinary methods and an object literal with method shorthand are checked strictly once the member they are assigned to is a property. What stays open, because it isn't an interface or a type literal:
- A class used as a type. A class's own methods are bivariant targets too. A subclass may override
handle(animal: Animal)withhandle(dog: Dog), anabstract handle(animal: Animal)may be implemented withhandle(dog: Dog), and a variable typed with the class (const h: AnimalHandler = new DogHandler()) accepts an instance whose method takes less.override-parameterreports an override (a method or an arrow function property, of a class or abstract method of the program) whose parameter asks for more than the overridden one's; an unrelated class put where a class type is expected is still open: pass objects around as an interface with property members rather than as a class type. - The platform's and packages' types, which are declared with methods:
Dog[]is accepted as anAnimal[](becausepushis a method), and so areMap<string, Dog>asMap<string, Animal>andSet<Dog>asSet<Animal>; pushing a cat into the wider view puts it in the dogs' array.readonly Dog[],ReadonlyMapandReadonlySetonly read, so widening them is sound: take the read-only view where code doesn't change the collection. - A mapped type over a method-form type (
Readonly<Handler>whenHandlerstill has a method) keeps the method's bivariance; the rule reports the method inHandleritself.
Not reported
- a method without parameters (
destroy(): void,channels(): readonly T[]): there is no parameter to check in either direction, so the property form closes nothing. Athisparameter (reset(this: Store): void) doesn't count, since the caller doesn't pass it. Overloads are one member: reported, every signature of it, when any of them has a parameter; - getters and setters (
get size(): number), and call and construct signatures, which TypeScript checks strictly; - members of an ambient block or declaration (
declare module "x",declare global,declare namespace,declare function,declare const,declare class): they describe or merge into types defined elsewhere, where a property can't replace a method the original declares; .d.tsfiles: they describe what a package or generated code provides.
Quick fix
Write handle as a readonly property with a function type keeps the parameters, type parameters (map<U>(f: (t: T) => U): U[] becomes readonly map: <U>(f: (t: T) => U) => U[]), a this parameter and the return type. Overloads (on(event: "open", ...), on(event: "data", ...)) become one property whose type lists the call signatures (readonly on: { (event: "open", ...): void; (event: "data", ...): void }), also when only some of them have parameters. A method without a return type gets no fix. safe-ts fix no-method-signature src --write converts a codebase; nothing runs differently, but an assignment that only compiled because of the bivariance now fails to compile, which is the point.
readonly is part of the fix: a method signature can be reassigned (h.handle = other), and code that does that stops compiling. Remove readonly there if the reassignment is meant.
What the rule can't see
- The class and library cases above.
- An
anyor an unchecked cast in between:no-anyandno-unchecked-castreport those.