typescript/no-unnecessary-type-constraint Suspicious
它的作用
禁止对泛型类型使用不必要的约束。
为什么这不好?
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>() {}
}如何使用
To enable this rule using the config file or in the CLI, you can use:
json
{
"rules": {
"typescript/no-unnecessary-type-constraint": "error"
}
}ts
import { defineConfig } from "oxlint";
export default defineConfig({
rules: {
"typescript/no-unnecessary-type-constraint": "error",
},
});bash
oxlint --deny typescript/no-unnecessary-type-constraint版本
此规则于 v0.0.6 中加入。
