no-unsafe-assignment
Disallow assigning a value with type
any
to variables and properties.
该规则需要 类型信息 才能运行,但这会带来性能方面的权衡。
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.
Assigning an any
typed value to a variable can be hard to pick up on, particularly if it leaks in from an external library.
此规则不允许将 any
分配给变量,并将 any[]
分配给数组解构。
¥This rule disallows assigning any
to a variable, and assigning any[]
to an array destructuring.
此规则还比较泛型类型参数类型,以确保你不会将通用位置中的不安全 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 assign Set<any>
to a variable declared as Set<string>
.
- Flat Config
- Legacy Config
export default tseslint.config({
rules: {
"@typescript-eslint/no-unsafe-assignment": "error"
}
});
module.exports = {
"rules": {
"@typescript-eslint/no-unsafe-assignment": "error"
}
};
在线运行试试这个规则 ↗
示例
¥Examples
- ❌ Incorrect
- ✅ Correct
const x = 1 as any,
y = 1 as any;
const [x] = 1 as any;
const [x] = [] as any[];
const [x] = [1 as any];
[x] = [1] as [any];
function foo(a = 1 as any) {}
class Foo {
constructor(private a = 1 as any) {}
}
class Foo {
private a = 1 as any;
}
// generic position examples
const x: Set<string> = new Set<any>();
const x: Map<string, string> = new Map<string, any>();
const x: Set<string[]> = new Set<any[]>();
const x: Set<Set<Set<string>>> = new Set<Set<Set<any>>>();
Open in Playgroundconst x = 1,
y = 1;
const [x] = [1];
[x] = [1] as [number];
function foo(a = 1) {}
class Foo {
constructor(private a = 1) {}
}
class Foo {
private a = 1;
}
// generic position examples
const x: Set<string> = new Set<string>();
const x: Map<string, string> = new Map<string, string>();
const x: Set<string[]> = new Set<string[]>();
const x: Set<Set<Set<string>>> = new Set<Set<Set<string>>>();
Open in Playground有些情况下规则允许将 any
分配给 unknown
。
¥There are cases where the rule allows assignment of any
to unknown
.
允许的 any
到 unknown
分配示例:
¥Example of any
to unknown
assignment that are allowed:
const x: unknown = y as any;
const x: unknown[] = y as any[];
const x: Set<unknown> = y as Set<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.
相关
¥Related To
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.
'## 资源'