@typescript-eslint/prefer-function-type
Require a function type instead of an interface or object type with only a call signature
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:
bunfig.toml
[lint.rules]
"@typescript-eslint/prefer-function-type" = "error" # or "warn"Examples#
Reported:
interface Handler {
(event: Event): void;
}
function listen(callback: { (event: Event): void }) {}Not reported:
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): this;
}