noUselessTypeConstraint
诊断类别:lint/complexity/noUselessTypeConstraint
¥Diagnostic Category: lint/complexity/noUselessTypeConstraint
自从:v1.0.0
¥Since: v1.0.0
来源:
¥Sources:
禁止使用 any
或 unknown
作为类型约束。
¥Disallow using any
or unknown
as type constraint.
TypeScript 中的通用类型参数(<T>
)可能会受到 extends
的限制。提供的类型必须是提供的约束的子类型。所有类型都是 any
和 unknown
的子类型。因此,从 any
或 unknown
扩展是无用的。
¥Generic type parameters (<T>
) in TypeScript may be constrained with extends
.
A supplied type must then be a subtype of the supplied constraint.
All types are subtypes of any
and unknown
.
It is thus useless to extend from any
or unknown
.
¥Examples
¥Invalid
code-block.ts:1:20 lint/complexity/noUselessTypeConstraint FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Constraining a type parameter to any or unknown is useless.
> 1 │ interface FooAny<T extends any> {}
│ ^^^^^^^^^^^
2 │
ℹ All types are subtypes of any and unknown.
ℹ Safe fix: Remove the constraint.
1 │ interface·FooAny<T·extends·any>·{}
│ ------------
code-block.ts:1:15 lint/complexity/noUselessTypeConstraint FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Constraining a type parameter to any or unknown is useless.
> 1 │ type BarAny<T extends any> = {};
│ ^^^^^^^^^^^
2 │
ℹ All types are subtypes of any and unknown.
ℹ Safe fix: Remove the constraint.
1 │ type·BarAny<T·extends·any>·=·{};
│ ------------
code-block.ts:1:16 lint/complexity/noUselessTypeConstraint FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Constraining a type parameter to any or unknown is useless.
> 1 │ class BazAny<T extends any> {
│ ^^^^^^^^^^^
2 │ }
3 │
ℹ All types are subtypes of any and unknown.
ℹ Safe fix: Remove the constraint.
1 │ class·BazAny<T·extends·any>·{
│ ------------
code-block.ts:2:12 lint/complexity/noUselessTypeConstraint FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Constraining a type parameter to any or unknown is useless.
1 │ class BazAny {
> 2 │ quxAny<U extends any>() {}
│ ^^^^^^^^^^^
3 │ }
4 │
ℹ All types are subtypes of any and unknown.
ℹ Safe fix: Remove the constraint.
2 │ ··quxAny<U·extends·any>()·{}
│ ------------
code-block.ts:1:20 lint/complexity/noUselessTypeConstraint FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Constraining a type parameter to any or unknown is useless.
> 1 │ const QuuxAny = <T extends any>() => {};
│ ^^^^^^^^^^^
2 │
ℹ All types are subtypes of any and unknown.
ℹ Safe fix: Remove the constraint.
1 │ const·QuuxAny·=·<T·extends·any>()·=>·{};
│ ------------
code-block.ts:1:20 lint/complexity/noUselessTypeConstraint FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Constraining a type parameter to any or unknown is useless.
> 1 │ function QuuzAny<T extends any>() {}
│ ^^^^^^^^^^^
2 │
ℹ All types are subtypes of any and unknown.
ℹ Safe fix: Remove the constraint.
1 │ function·QuuzAny<T·extends·any>()·{}
│ ------------
code-block.ts:1:24 lint/complexity/noUselessTypeConstraint FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Constraining a type parameter to any or unknown is useless.
> 1 │ interface FooUnknown<T extends unknown> {}
│ ^^^^^^^^^^^^^^^
2 │
ℹ All types are subtypes of any and unknown.
ℹ Safe fix: Remove the constraint.
1 │ interface·FooUnknown<T·extends·unknown>·{}
│ ----------------
code-block.ts:1:19 lint/complexity/noUselessTypeConstraint FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Constraining a type parameter to any or unknown is useless.
> 1 │ type BarUnknown<T extends unknown> = {};
│ ^^^^^^^^^^^^^^^
2 │
ℹ All types are subtypes of any and unknown.
ℹ Safe fix: Remove the constraint.
1 │ type·BarUnknown<T·extends·unknown>·=·{};
│ ----------------
code-block.ts:3:4 parse ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ unterminated template literal
1 │ class BazUnknown<T extends unknown> {
2 │ }
> 3 │ ```ts,expect_diagnostic
│ ^^^^^^^^^^^^^^^^^^^^
> 4 │ class BazUnknown {
> 5 │ quxUnknown<U extends unknown>() {}
> 6 │ }
> 7 │
│
code-block.ts:1:24 lint/complexity/noUselessTypeConstraint FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Constraining a type parameter to any or unknown is useless.
> 1 │ const QuuxUnknown = <T extends unknown>() => {};
│ ^^^^^^^^^^^^^^^
2 │
ℹ All types are subtypes of any and unknown.
ℹ Safe fix: Remove the constraint.
1 │ const·QuuxUnknown·=·<T·extends·unknown>()·=>·{};
│ ----------------
code-block.ts:1:24 lint/complexity/noUselessTypeConstraint FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Constraining a type parameter to any or unknown is useless.
> 1 │ function QuuzUnknown<T extends unknown>() {}
│ ^^^^^^^^^^^^^^^
2 │
ℹ All types are subtypes of any and unknown.
ℹ Safe fix: Remove the constraint.
1 │ function·QuuzUnknown<T·extends·unknown>()·{}
│ ----------------
¥Valid
¥Related links