@typescript-eslint/no-unnecessary-type-constraint
Disallow extends any and extends unknown on a type parameter
warn by defaulthas suggestions
Also called typescript-eslint/no-unnecessary-type-constraint, typescript/no-unnecessary-type-constraint
A type parameter without a constraint already accepts every type. <T extends unknown> and <T extends any> say the same thing with more words.
This rule is on by default in TypeScript files.
Configure#
On by default as a warning. To turn it off:
bunfig.toml
[lint.rules]
"@typescript-eslint/no-unnecessary-type-constraint" = "off" # or "error"Examples#
example.ts3 problems
function first<Item extends unknownConstraining the generic type Item to unknown does nothing and is unnecessary.>(items: Item[]) {
return items[0];
}
class Box<Value extends anyConstraining the generic type Value to any does nothing and is unnecessary.> {
constructor(public value: Value) {}
}
type Pair<Left, Right extends unknownConstraining the generic type Right to unknown does nothing and is unnecessary.> = [Left, Right];example.tsNo problems
function first<Item>(items: Item[]) {
return items[0];
}
class Box<Value> {
constructor(public value: Value) {}
}
function longest<Item extends { length: number }>(a: Item, b: Item) {
return a.length >= b.length ? a : b;
}In a .tsx file, <Item>(item: Item) => item reads as a JSX tag. Write a trailing comma instead of extends unknown:
example.tsxNo problems
const identity = <Item,>(item: Item) => item;