Skip to content

Commit

Permalink
docs: [no-unnecessary-type-parameters] add FAQ section (#9975)
Browse files Browse the repository at this point in the history
* [no-unnecessary-type-parameters] add FAQ section

* wording

* wording

* update admonition link

* test case

* stuf

* prettierer

* test case formatting

* log

* mention 9735

* explain the equal false positives a little better

* add implicit return type FAQ
  • Loading branch information
kirkwaiblinger authored Sep 23, 2024
1 parent e8555a0 commit dc1c6d3
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 2 deletions.
123 changes: 122 additions & 1 deletion packages/eslint-plugin/docs/rules/no-unnecessary-type-parameters.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ 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](#limitations) section below and our [issue tracker](https://github.com/typescript-eslint/typescript-eslint/issues?q=is%3Aissue+no-unnecessary-type-parameters).
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](#limitations) and [FAQ](#faq) sections below and our [issue tracker](https://github.com/typescript-eslint/typescript-eslint/issues?q=is%3Aissue+no-unnecessary-type-parameters).
If you don't see your case covered, please [reach out to us](https://typescript-eslint.io/contributing/issues)!
:::

Expand Down Expand Up @@ -87,6 +87,127 @@ This is because the type parameter `T` relates multiple methods in the `T[]` tog
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...

```ts
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](#limitations) 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:

```ts
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:

```ts
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](https://github.com/typescript-eslint/typescript-eslint/issues/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.

```ts
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:

```ts
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](https://www.typescriptlang.org/docs/handbook/2/objects.html#excess-property-checks) for further discussion.

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

1. 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.
2. 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](https://www.typescriptlang.org/docs/handbook/2/objects.html#index-signatures):

```ts
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](https://www.typescriptlang.org/docs/handbook/utility-types.html#noinfertype) (see [#9751](https://github.com/typescript-eslint/typescript-eslint/issues/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](https://github.com/typescript-eslint/typescript-eslint/issues/9709)):

{/* prettier-ignore */}
```ts
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.

{/* TODO - include an FAQ entry regarding instantiation expressions once the conversation in https://github.com/typescript-eslint/typescript-eslint/pull/9536#discussion_r1705850744 is done */}

## When Not To Use It

This rule will report on functions that use type parameters solely to test types, for example:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { RuleTester } from '@typescript-eslint/rule-tester';
import { noFormat, RuleTester } from '@typescript-eslint/rule-tester';

import rule from '../../src/rules/no-unnecessary-type-parameters';
import { getFixturesRootDir } from '../RuleTester';
Expand Down Expand Up @@ -970,5 +970,31 @@ const f = <T,>(
},
],
},
{
// This isn't actually an important test case.
// However, we use it as an example in the docs of code that is flagged,
// but shouldn't necessarily be. So, if you make a change to the rule logic
// that resolves this sort-of-false-positive, please update the docs
// accordingly.
// Original discussion in https://github.com/typescript-eslint/typescript-eslint/issues/9709
code: noFormat`
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;
`,
errors: [
{
messageId: 'sole',
data: { descriptor: 'function', name: 'T1', uses: 'used only once' },
},
{
messageId: 'sole',
data: { descriptor: 'function', name: 'T2', uses: 'used only once' },
},
],
},
],
});

0 comments on commit dc1c6d3

Please sign in to comment.