@typescript-eslint/prefer-function-type
Require a function type instead of an interface or object type with only a call signature
Also called typescript-eslint/prefer-function-type, typescript/prefer-function-type
An interface with a call signature and nothing else describes a function. type Handler = (event: Event) => void says the same thing in one line.
Inline object types and new (...) signatures are checked too. An interface that extends another interface is left alone.
--fix rewrites the interface to a type alias, and the object type to a function type.
Configure#
Off by default. To turn it on:
[lint.rules]
"@typescript-eslint/prefer-function-type" = "error" # or "warn"Examples#
interface Handler {
(event: Event): void;Interface only has a call signature, you should use a function type instead.
}
function listen(callback: { (event: Event): voidType literal only has a call signature, you should use a function type instead. }) {}type Handler = (event: Event) => void;
function listen(callback: (event: Event) => void) {}A type with another member, or with more than one call signature, can't be written as a function type:
interface Logger {
(message: string): void;
level: number;
}
interface Parse {
(text: string): object;
(text: string, strict: boolean): object;
}
interface Timed extends Logger {
(message: string): void;
}In an interface, this as a type means the interface itself. A function type has no such this, so the rule points at it and doesn't offer a fix:
interface Chain {
(step: string): thisthis refers to the function type Chain, did you intend to use a generic this parameter like <Self>(this: Self, ...) => Self instead?;
}