noUselessTypeConstraint
Summary
Section titled “Summary”- Rule available since:
v1.0.0 - Diagnostic Category:
lint/complexity/noUselessTypeConstraint - This rule is recommended, which means is enabled by default.
- This rule has a safe fix.
- The default severity of this rule is information.
- Sources:
How to configure
Section titled “How to configure”{ "linter": { "rules": { "complexity": { "noUselessTypeConstraint": "error" } } }}Description
Section titled “Description”Disallow using any or unknown as type constraint.
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
Section titled “Examples”Invalid
Section titled “Invalid”interface FooAny<T extends any> {}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>·{}
│ ------------
type BarAny<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>·=·{};
│ ------------
class BazAny<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>·{
│ ------------
class BazAny { quxAny<U 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>()·{}
│ ------------
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 │ 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>()·=>·{};
│ ------------
function QuuzAny<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>()·{}
│ ------------
interface FooUnknown<T extends unknown> {}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>·{}
│ ----------------
type BarUnknown<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>·=·{};
│ ----------------
class BazUnknown<T extends unknown> {}code-block.ts:1:20 lint/complexity/noUselessTypeConstraint FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Constraining a type parameter to any or unknown is useless.
> 1 │ class BazUnknown<T extends unknown> {
│ ^^^^^^^^^^^^^^^
2 │ }
3 │
ℹ All types are subtypes of any and unknown.
ℹ Safe fix: Remove the constraint.
1 │ class·BazUnknown<T·extends·unknown>·{
│ ----------------
class BazUnknown { quxUnknown<U extends unknown>() {}}code-block.ts:2:16 lint/complexity/noUselessTypeConstraint FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Constraining a type parameter to any or unknown is useless.
1 │ class BazUnknown {
> 2 │ quxUnknown<U extends unknown>() {}
│ ^^^^^^^^^^^^^^^
3 │ }
4 │
ℹ All types are subtypes of any and unknown.
ℹ Safe fix: Remove the constraint.
2 │ ··quxUnknown<U·extends·unknown>()·{}
│ ----------------
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 │ 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>()·=>·{};
│ ----------------
function QuuzUnknown<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>()·{}
│ ----------------
interface Foo<T> {}
type Bar<T> = {};Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.