no-unnecessary-type-parameters
Disallow type parameters that aren't used multiple times.
在 ESLint 配置 中扩展"plugin:@typescript-eslint/strict-type-checked"
可启用此规则。
此规则报告的一些问题可以通过编辑器 建议 手动修复。
该规则需要 类型信息 才能运行,但这会带来性能方面的权衡。
此规则禁止在函数、方法或类定义中未多次使 用的类型参数。
¥This rule forbids type parameters that aren't used multiple times in a function, method, or class definition.
类型参数与两种类型相关。如果类型参数仅使用一次,则它与任何内容都无关。它通常可以用显式类型(如 unknown
)替换。
¥Type parameters relate two types.
If a type parameter is only used once, then it is not relating anything.
It can usually be replaced with explicit types such as unknown
.
在最好的情况下,不必要的类型参数会使代码更难阅读。在最坏的情况下,它们可用于伪装不安全的类型断言。
¥At best unnecessary type parameters make code harder to read. At worst they can be used to disguise unsafe type assertions.
此规则是最近添加的,与我们的大多数规则相比,它具有令人惊讶的隐藏复杂性。如果你遇到意外行为,请仔细检查下面的 局限性 和 常见问题 部分以及我们的 问题跟踪器。如果你认为你的案例没有被覆盖,请使用 联系我们!
¥This rule was recently added, and has a surprising amount of hidden complexity compared to most of our rules. If you encounter unexpected behavior with it, please check closely the Limitations and FAQ sections below and our issue tracker. If you don't see your case covered, please reach out to us!
- Flat Config
- Legacy Config
export default tseslint.config({
rules: {
"@typescript-eslint/no-unnecessary-type-parameters": "error"
}
});
module.exports = {
"rules": {
"@typescript-eslint/no-unnecessary-type-parameters": "error"
}
};
在线运行试试这个规则 ↗
示例
¥Examples
- ❌ Incorrect
- ✅ Correct
function second<A, B>(a: A, b: B): B {
return b;
}
function parseJSON<T>(input: string): T {
return JSON.parse(input);
}
function printProperty<T, K extends keyof T>(obj: T, key: K) {
console.log(obj[key]);
}
Open in Playgroundfunction second<B>(a: unknown, b: B): B {
return b;
}
function parseJSON(input: string): unknown {
return JSON.parse(input);
}
function printProperty<T>(obj: T, key: keyof T) {
console.log(obj[key]);
}
// T appears twice: in the type of arg and as the return type
function identity<T>(arg: T): T {
return arg;
}
// T appears twice: "keyof T" and in the inferred return type (T[K]).
// K appears twice: "key: K" and in the inferred return type (T[K]).
function getProperty<T, K extends keyof T>(obj: T, key: K) {
return obj[key];
}
Open in Playground局限性
¥Limitations
请注意,此规则允许多次使用任何类型参数,即使这些使用是通过类型参数进行的。例如,以下 T
由于位于 Array
中而被多次使用,即使其名称在声明后仅出现一次:
¥Note that this rule allows any type parameter that is used multiple times, even if those uses are via a type argument.
For example, the following T
is used multiple times by virtue of being in an Array
, even though its name only appears once after declaration:
declare function createStateHistory<T>(): T[];
这是因为类型参数 T
将 T[]
(Array<T>
)中的多个方法关联在一起,使其多次使用。
¥This is because the type parameter T
relates multiple methods in T[]
(Array<T>
) together, making it used more than once.
因此,此规则不会报告用作类型参数的类型参数。这包括提供给全局类型(例如 Array
、Map
和 Set
)的类型参数,这些类型参数具有多个方法和属性,可以根据类型参数更改值。
¥Therefore, this rule won't report on type parameters used as a type argument.
This includes type arguments provided to global types such as Array
, Map
, and Set
that have multiple methods and properties that can change values based on the type parameter.
另一方面,只读和固定数组类(例如 readonly T[]
、ReadonlyArray
)和元组(例如 [T]
)是特殊情况,在用作输入类型或 readonly
输出类型时会特别报告。由于 T
仅用作 ReadonlyArray
全局类型的类型参数一次,因此将报告以下示例:
¥On the other hand, readonly and fixed array-likes such as readonly T[]
, ReadonlyArray
, and tuples such as [T]
are special cases that are specifically reported on when used as input types, or as readonly
output types.
The following example will be reported because T
is used only once as type argument for the ReadonlyArray
global type:
- ❌ Incorrect
- ✅ Correct
declare function length<T>(array: ReadonlyArray<T>): number;
Open in Playgrounddeclare function length(array: ReadonlyArray<unknown>): number;
Open in Playground常见问题
¥FAQ