noBannedTypes
诊断类别:lint/complexity/noBannedTypes
¥Diagnostic Category: lint/complexity/noBannedTypes
自从:v1.0.0
¥Since: v1.0.0
来源:
¥Sources:
-
与以下相同:
@typescript-eslint/ban-types
¥Same as:
@typescript-eslint/ban-types
禁止原始类型别名和误导性类型。
¥Disallow primitive type aliases and misleading types.
-
检查非零长度时,强制与 > 0 进行比较。
¥Enforce consistent names for primitive types
原始类型有别名。例如,Number
是 number
的别名。规则建议使用小写的原始类型名称。
¥Primitive types have aliases.
For example, Number
is an alias of number
.
The rule recommends the lowercase primitive type names.
-
禁止
Function
类型¥Disallow the
Function
type
Function
类型是松散类型的,因此被认为是危险或有害的。Function
相当于使用不安全 any
类型的类型 (...rest: any[]) => any
。
¥The Function
type is loosely typed and is thus considered dangerous or harmful.
Function
is equivalent to the type (...rest: any[]) => any
that uses the unsafe any
type.
-
禁止误导性的非可空类型
{}
¥Disallow the misleading non-nullable type
{}
在 TypeScript 中,类型 {}
不代表空对象。它表示除 null
和 undefined
之外的任何值。以下 TypeScript 示例完全有效:
¥In TypeScript, the type {}
doesn’t represent an empty object.
It represents any value except null
and undefined
.
The following TypeScript example is perfectly valid:
code-block.ts:1:10 lint/complexity/noBannedTypes ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Don’t use ’{}’ as a type.
> 1 │ const n: {} = 0
│ ^^
2 │
ℹ Prefer explicitly define the object shape. ’{}’ means “any non-nullable value”.
要表示空对象,你应该使用 { [k: string]: never }
或 Record<string, never>
。
¥To represent an empty object, you should use { [k: string]: never }
or Record<string, never>
.
为了避免任何混淆,规则禁止使用类型 {}
,但两种情况除外:
¥To avoid any confusion, the rule forbids the use of the type {}
, except in two situations:
-
在类型约束中将泛型限制为非可空类型:
¥In type constraints to restrict a generic type to non-nullable types:
-
在类型交集中将类型缩小为其不可空的等效类型:
¥In a type intersection to narrow a type to its non-nullable equivalent type:
在最后一种情况下,你还可以使用 NonNullable
工具类型:
¥In this last case, you can also use the NonNullable
utility type:
¥Examples
¥Invalid
code-block.ts:1:10 lint/complexity/noBannedTypes FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Don’t use ‘String’ as a type.
> 1 │ let foo: String = “bar”;
│ ^^^^^^
2 │
ℹ Use lowercase primitives for consistency.
ℹ Safe fix: Use ‘string’ instead
1 │ - let·foo:·String·=·“bar”;
1 │ + let·foo:·string·=·“bar”;
2 2 │
code-block.ts:1:20 lint/complexity/noBannedTypes FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Don’t use ‘Boolean’ as a type.
> 1 │ let bool = true as Boolean;
│ ^^^^^^^
2 │
ℹ Use lowercase primitives for consistency.
ℹ Safe fix: Use ‘boolean’ instead
1 │ - let·bool·=·true·as·Boolean;
1 │ + let·bool·=·true·as·boolean;
2 2 │
code-block.ts:1:28 lint/complexity/noBannedTypes FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Don’t use ‘Boolean’ as a type.
> 1 │ let invalidTuple: [string, Boolean] = [“foo”, false];
│ ^^^^^^^
2 │
ℹ Use lowercase primitives for consistency.
ℹ Safe fix: Use ‘boolean’ instead
1 │ - let·invalidTuple:·[string,·Boolean]·=·[“foo”,·false];
1 │ + let·invalidTuple:·[string,·boolean]·=·[“foo”,·false];
2 2 │
¥Valid
¥Related links