Disallow calling a value with type
any
.
💭
This rule requires type information to run, which comes with performance tradeoffs.
The any
type in TypeScript is a dangerous "escape hatch" from the type system. Using any
disables many type checking rules and is generally best used only as a last resort or when prototyping code.
Despite your best intentions, the any
type can sometimes leak into your codebase. Calling an any
-typed value as a function creates a potential type safety hole and source of bugs in your codebase.
This rule disallows calling any value that is typed as any
.
eslint.config.mjs
export default tseslint.config({
rules: {
"@typescript-eslint/no-unsafe-call": "error"
}
});
.eslintrc.cjs
module.exports = {
"rules": {
"@typescript-eslint/no-unsafe-call": "error"
}
};
Try this rule in the playground ↗
Examplesdeclare const anyVar: any;
declare const nestedAny: { prop: any };
anyVar();
anyVar.a.b();
nestedAny.prop();
nestedAny.prop['a']();
new anyVar();
new nestedAny.prop();
anyVar`foo`;
nestedAny.prop`foo`;
Open in Playground
declare const typedVar: () => void;
declare const typedNested: { prop: { a: () => void } };
typedVar();
typedNested.prop.a();
(() => {})();
new Map();
String.raw`foo`;
Open in Playground The Unsafe Function
Type
The Function
type is behaves almost identically to any
when called, so this rule also disallows calling values of type Function
.
Note that whereas no-unsafe-function-type helps prevent the creation of Function
types, this rule helps prevent the unsafe use of Function
types, which may creep into your codebase without explicitly referencing the Function
type at all. See, for example, the following code:
function callUnsafe(maybeFunction: unknown): string {
if (typeof maybeFunction === 'function') {
return maybeFunction('call', 'with', 'any', 'args');
}
}
In this sort of situation, beware that there is no way to guarantee with runtime checks that a value is safe to call. If you really want to call a value whose type you don't know, your best best is to use a try
/catch
and suppress any TypeScript or linter errors that get in your way.
function callSafe(maybeFunction: unknown): void {
try {
(maybeFunction as () => unknown)();
} catch (e) {
console.error(
'Function either could not be called or threw an error when called: ',
e,
);
}
}
Options
This rule is not configurable.
When Not To Use ItIf your codebase has many existing any
s or areas of unsafe code, it may be difficult to enable this rule. It may be easier to skip the no-unsafe-*
rules pending increasing type safety in unsafe areas of your project. You might consider using ESLint disable comments for those specific situations instead of completely disabling this rule.
any
s with Linting and TypeScriptno-explicit-any
no-unsafe-argument
no-unsafe-assignment
no-unsafe-member-access
no-unsafe-return
Type checked lint rules are more powerful than traditional lint rules, but also require configuring type checked linting.
See Troubleshooting > Linting with Type Information > Performance if you experience performance degradations after enabling type checked rules.
ResourcesRetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4