noExplicitAny
¥Summary
-
规则生效日期:
v1.0.0¥Rule available since:
v1.0.0 -
诊断类别:
lint/suspicious/noExplicitAny¥Diagnostic Category:
lint/suspicious/noExplicitAny -
此规则为推荐规则,默认启用。
¥This rule is recommended, which means is enabled by default.
-
此规则没有修复方案。
¥This rule doesn’t have a fix.
-
此规则的默认严重级别为 warning。
¥The default severity of this rule is warning.
-
来源:
¥Sources:
¥How to configure
{ "linter": { "rules": { "suspicious": { "noExplicitAny": "error" } } }}¥Description
禁止使用 any 类型。
¥Disallow the any type usage.
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.
TypeScript 的 --noImplicitAny 编译器选项可防止隐含的 any,但不会阻止 any 按照此规则的方式显式使用。
¥TypeScript’s --noImplicitAny compiler option prevents an implied any,
but doesn’t prevent any from being explicitly used the way this rule does.
有时你可以使用类型 unknown 而不是类型 any。它还接受任何值,但是需要在调用属性之前检查属性是否存在。
¥Sometimes you can use the type unknown instead of the type any.
It also accepts any value, however it requires to check that a property exists before calling it.
¥Examples
¥Invalid
let variable: any = 1;code-block.ts:1:15 lint/suspicious/noExplicitAny ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ Unexpected any. Specify a different type.
> 1 │ let variable: any = 1;
│ ^^^
2 │
ℹ any disables many type checking rules. Its use should be avoided.
class SomeClass { message: Array<Array<any>>;}code-block.ts:2:25 lint/suspicious/noExplicitAny ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ Unexpected any. Specify a different type.
1 │ class SomeClass {
> 2 │ message: Array<Array<any>>;
│ ^^^
3 │ }
4 │
ℹ any disables many type checking rules. Its use should be avoided.
function fn(param: Array<any>): void {}code-block.ts:1:26 lint/suspicious/noExplicitAny ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ Unexpected any. Specify a different type.
> 1 │ function fn(param: Array<any>): void {}
│ ^^^
2 │
ℹ any disables many type checking rules. Its use should be avoided.
¥Valid
let variable: number = 1;let variable2 = 1;class SomeClass<T extends any> { message: Array<Array<unknown>>;}function fn(param: Array<Array<unknown>>): Array<unknown> {}¥Related links
Biome v2.1 中文网 - 粤ICP备13048890号