no-unsafe-argument
Disallow calling a function with a value with type
any
.
该规则需要 类型信息 才能运行,但这会带来性能方面的权衡。
TypeScript 中的 any
类型是来自类型系统的危险 "应急方案"。使用 any
会禁用许多类型检查规则,通常最好只作为最后的手段或在原型代码时使用。
¥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.
尽管你有最好的意图,但 any
类型有时会泄漏到你的代码库中。使用 any
类型参数调用函数会产生潜在的安全漏洞和错误源。
¥Despite your best intentions, the any
type can sometimes leak into your codebase.
Calling a function with an any
typed argument creates a potential safety hole and source of bugs.
此规则不允许调用参数中包含 any
的函数。这包括将数组或元组与 any
类型元素作为函数参数展开。
¥This rule disallows calling a function with any
in its arguments.
That includes spreading arrays or tuples with any
typed elements as function arguments.
此规则还比较泛型类型参数类型,以确保你不会将通用位置中的不安全 any
传递给期望特定类型的接收者。例如,如果将 Set<any>
作为参数传递给声明为 Set<string>
的参数,则会出错。
¥This rule also compares generic type argument types to ensure you don't pass an unsafe any
in a generic position to a receiver that's expecting a specific type.
For example, it will error if you pass Set<any>
as an argument to a parameter declared as Set<string>
.
- Flat Config
- Legacy Config
export default tseslint.config({
rules: {
"@typescript-eslint/no-unsafe-argument": "error"
}
});
module.exports = {
"rules": {
"@typescript-eslint/no-unsafe-argument": "error"
}
};
在线运行试试这个规则 ↗
示例
¥Examples
- ❌ Incorrect
- ✅ Correct
declare function foo(arg1: string, arg2: number, arg3: string): void;
const anyTyped = 1 as any;
foo(...anyTyped);
foo(anyTyped, 1, 'a');
const anyArray: any[] = [];
foo(...anyArray);
const tuple1 = ['a', anyTyped, 'b'] as const;
foo(...tuple1);
const tuple2 = [1] as const;
foo('a', ...tuple2, anyTyped);
declare function bar(arg1: string, arg2: number, ...rest: string[]): void;
const x = [1, 2] as [number, ...number[]];
foo('a', ...x, anyTyped);
declare function baz(arg1: Set<string>, arg2: Map<string, string>): void;
foo(new Set<any>(), new Map<any, string>());
Open in Playgrounddeclare function foo(arg1: string, arg2: number, arg3: string): void;
foo('a', 1, 'b');
const tuple1 = ['a', 1, 'b'] as const;
foo(...tuple1);
declare function bar(arg1: string, arg2: number, ...rest: string[]): void;
const array: string[] = ['a'];
bar('a', 1, ...array);
declare function baz(arg1: Set<string>, arg2: Map<string, string>): void;
foo(new Set<string>(), new Map<string, string>());
Open in Playground有些情况下规则允许将 any
的参数传递给 unknown
。
¥There are cases where the rule allows passing an argument of any
to unknown
.
允许的 any
到 unknown
分配示例:
¥Example of any
to unknown
assignment that are allowed:
declare function foo(arg1: unknown, arg2: Set<unknown>, arg3: unknown[]): void;
foo(1 as any, new Set<any>(), [] as any[]);
Open in Playground选项
该规则不可配置。
何时不使用它
¥When Not To Use It
如果你的代码库有许多现有的 any
或不安全代码区域,则可能很难启用此规则。在项目不安全区域中增加类型安全性时,跳过 no-unsafe-*
规则可能会更容易。你可以考虑在这些特定情况下使用 ESLint 禁用注释,而不是完全禁用此规则。
¥If 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.