typescript/no-unnecessary-type-constraint 可疑
它的作用
禁止在泛型类型上使用不必要的约束。
为什么这是个问题?
TypeScript 中的泛型类型参数(<T>)可以使用 extends 关键字进行“约束”。当未提供 extends 时,类型参数默认约束为 unknown。因此,从 any 或 unknown 继承是冗余的。
示例
此规则的 错误 代码示例:
typescript
interface FooAny<T extends any> {}
interface FooUnknown<T extends unknown> {}
type BarAny<T extends any> = {};
type BarUnknown<T extends unknown> = {};
const QuuxAny = <T extends any>() => {};
function QuuzAny<T extends any>() {}typescript
class BazAny<T extends any> {
quxAny<U extends any>() {}
}此规则的 正确 代码示例:
typescript
interface Foo<T> {}
type Bar<T> = {};
const Quux = <T>() => {};
function Quuz<T>() {}typescript
class Baz<T> {
qux<U>() {}
}如何使用
要通过配置文件或 CLI 启用 此规则,可以使用:
json
{
"rules": {
"typescript/no-unnecessary-type-constraint": "error"
}
}bash
oxlint --deny typescript/no-unnecessary-type-constraint