添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
叛逆的青蛙  ·  Java ...·  1小时前    · 
酷酷的茶叶  ·  什么是 WebSocket ...·  4 小时前    · 
谦虚好学的太阳  ·  SVA 纽约视觉 - ...·  2 月前    · 
好帅的砖头  ·  How to use CASE in ...·  1 年前    · 
帅呆的茴香  ·  count parameter ...·  1 年前    · 
Skip to main content

no-unnecessary-type-parameters

Disallow type parameters that aren't used multiple times.

💡

Some problems reported by this rule are manually fixable by editor suggestions .

💭

This rule requires type information to run.

This rule forbids type parameters that aren't used multiple times in a function, method, or class definition.

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.

warning

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 !

.eslintrc.cjs
module.exports = {
"rules": {
"@typescript-eslint/no-unnecessary-type-parameters": "error"
}
};

Try this rule in the playground ↗

Examples

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 Playground

Limitations

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[];

This is because the type parameter T relates multiple methods in the T[] together, making it used more than once.

Therefore, this rule won't report on type parameters used as a type argument. That includes type arguments given to global types such as Array (including the T[] shorthand and in tuples), Map , and Set .

FAQ

The return type is only used as an input, so why isn't the rule reporting?

One common reason that this might be the case is when the return type is not specified explicitly. The rule uses uses type information to count implicit usages of the type parameter in the function signature, including in the inferred return type. For example, the following function...

function identity<T>(arg: T) {
return arg;
}

...implicitly has a return type of T . Therefore, the type parameter T is used twice, and the rule will not report this function.

For other reasons the rule might not be reporting, be sure to check the Limitations section and other FAQs.

I'm using the type parameter inside the function, so why is the rule reporting?

You might be surprised to that the rule reports on a function like this:

function log<T extends string>(string1: T): void {
const string2: T = string1;
console.log(string2);
}

After all, the type parameter T relates the input string1 and the local variable string2 , right? However, this usage is unnecessary, since we can achieve the same results by replacing all usages of the type parameter with its constraint. That is to say, the function can always be rewritten as:

function log(string1: string): void {
const string2: string = string1;
console.log(string2);
}

Therefore, this rule only counts usages of a type parameter in the signature of a function, method, or class, but not in the implementation. See also #9735

Why am I getting TypeScript errors saying "Object literal may only specify known properties" after removing an unnecessary type parameter?

Suppose you have a situation like the following, which will trigger the rule to report.

interface SomeProperties {
foo: string;
}

// T is only used once, so the rule will report.
function serialize<T extends SomeProperties>(x: T): string {
return JSON.stringify(x);
}

serialize({ foo: 'bar', anotherProperty: 'baz' });

If we remove the unnecessary type parameter, we'll get an error:

function serialize(x: SomeProperties): string {
return JSON.stringify(x);
}

// TS Error: Object literal may only specify known properties, and 'anotherProperty' does not exist in type 'SomeProperties'.
serialize({ foo: 'bar', anotherProperty: 'baz' });

This is because TypeScript figures it's usually an error to explicitly provide excess properties in a location that expects a specific type. See the TypeScript handbook's section on excess property checks for further discussion.

To resolve this, you have two approaches to choose from.

If it doesn't make sense to accept excess properties in your function, you'll want to fix the errors at the call sites. Usually, you can simply remove any excess properties where the function is called.

Otherwise, if you do want your function to accept excess properties, you can modify the parameter type in order to allow excess properties explicitly by using an index signature :

interface SomeProperties {
foo: string;

// This allows any other properties.
// You may wish to make these types more specific according to your use case.
[key: PropertKey]: unknown;
}

function serialize(x: SomeProperties): string {
return JSON.stringify(x);
}

// No error!
serialize({ foo: 'bar', anotherProperty: 'baz' });

Which solution is appropriate is a case-by-case decision, depending on the intended use case of your function.

I have a complex scenario that is reported by the rule, but I can't see how to remove the type parameter. What should I do?

Sometimes, you may be able to rewrite the code by reaching for some niche TypeScript features, such as the NoInfer<T> utility type (see #9751 ).

But, quite possibly, you've hit an edge case where the type is being used in a subtle way that the rule doesn't account for. For example, the following arcane code is a way of testing whether two types are equal, and will be reported by the rule (see #9709 ):

type Compute<A> = A extends Function ? A : { [K in keyof A]: Compute<A[K]> };
type Equal<X, Y> =
(<T1>() => T1 extends Compute<X> ? 1 : 2) extends
(<T2>() => T2 extends Compute<Y> ? 1 : 2)
? true
: false;

In this case, the function types created within the Equal type are never expected to be assigned to; they're just created for the purpose of type system manipulations. This usage is not what the rule is intended to analyze.

Use eslint-disable comments as appropriate to suppress the rule in these kinds of cases.

Options

This rule is not configurable.

When Not To Use It

This rule will report on functions that use type parameters solely to test types, for example:

function assertType<T>(arg: T) {}

assertType<number>(123);
assertType<number>('abc');
// ~~~~~
// Argument of type 'string' is not assignable to parameter of type 'number'.

If you're using this pattern then you'll want to disable this rule on files that test types.

Further Reading

  • TypeScript handbook: Type Parameters Should Appear Twice
  • Effective TypeScript: The Golden Rule of Generics
  • eslint-plugin-etc's no-misused-generics
  • wotan's no-misused-generics
  • DefinitelyTyped-tools' no-unnecessary-generics
  • Type checked lint rules are more powerful than traditional lint rules, but also require configuring type checked linting .

    See Troubleshooting > Linting with Type Information > Performance if you experience performance degredations after enabling type checked rules.

    Resources