@typescript-eslint/no-unsafe-function-type
Disallow the built-in Function type
warn by default
Also called typescript-eslint/no-unsafe-function-type, typescript/no-unsafe-function-type
You can call a value of type Function with any arguments, and the result is any, so TypeScript checks nothing about the call. Function also accepts classes, which throw when you call them without new.
Write the parameters and the return type instead.
This rule is on by default in TypeScript files.
Configure#
On by default as a warning. To turn it off:
bunfig.toml
[lint.rules]
"@typescript-eslint/no-unsafe-function-type" = "off" # or "error"Examples#
example.ts1 problem
function onChange(listener: FunctionThe Function type accepts any function-like value.Prefer explicitly defining any function parameters and return type.) {
listener("name", 1, 2, 3);
}example.tsNo problems
function onChange(listener: (field: string) => void) {
listener("name");
}An interface that extends Function is reported too:
example.ts1 problem
interface Handler extends FunctionThe Function type accepts any function-like value.Prefer explicitly defining any function parameters and return type. {
priority: number;
}To accept any function at all, use a signature that every function matches:
example.tsNo problems
function nameOf(task: (...args: never[]) => unknown) {
return task.name;
}Your own type named Function isn't reported:
example.tsNo problems
import type { Function } from "./ast";
export function visit(node: Function) {}