@typescript-eslint/no-unsafe-call
Disallow calling a value of type any
Also called typescript-eslint/no-unsafe-call, typescript/no-unsafe-call
The compiler lets you call an any with any arguments. It doesn't check that the value is a function, and the result is any again. If the value isn't a function, you find out from a TypeError when the code runs.
The rule reports calls, new, and tagged templates. It also reports a value of type Function, which says you can call something but not which arguments it takes. A type that can't be resolved, for example from a missing import, counts as any.
bun lint --type-aware turns this rule on in TypeScript files.
Configure#
Off by default. This rule needs types, so turning it on makes bun lint type check your code first.
[lint.rules]
"@typescript-eslint/no-unsafe-call" = "error" # or "warn"Examples#
function run(plugin: any, handler: Function) {
pluginUnsafe call of an any typed value.();
plugin.setupUnsafe call of an any typed value.();
new plugin.Worker()Unsafe construction of an any typed value.;
handlerUnsafe call of a Function typed value.();
}interface Plugin {
setup(): void;
Worker: new () => object;
}
function run(plugin: Plugin, handler: () => void) {
plugin.setup();
new plugin.Worker();
handler();
}For a value you know nothing about, use unknown and check it before the call:
function stop(controller: unknown) {
if (controller instanceof AbortController) {
controller.abort();
}
}When not to use it#
In a codebase that's still moving from JavaScript to TypeScript, any is everywhere and the reports drown out everything else. Turn the rule on once most values have real types.