typescript-eslint: [no-unnecessary-type-constraint] false positive for generic type with default type on arrow function in a JSX/TSX file

  • I have tried restarting my IDE and the issue persists.
  • I have updated to the latest version of the packages.
  • I have read the FAQ and my problem is not listed.

Repro https://github.com/AllySummers/typescript-eslint-jsx-generics Repo has eslint v8 and typescript-eslint v5, issue still happens on eslint 7 and typescript-eslint 4 (including alpha). To install eslint v7/typescript-eslint v4 in that repo do this: npm i --save @typescript-eslint/eslint-plugin@4.33.1-alpha.15 @typescript-eslint/eslint-plugin@4.33.1-alpha.15 eslint@7.32.0

The issue can also be seen from running npm run lint

not-working.tsx

export interface GenericComponentProps<T> {
  prop: T
}

export const GenericComponent = <T>(props: GenericComponentProps<T>) => (
  <div>hello</div>
);
// Error: 5:70  error  Parsing error: Unexpected token. Did you mean `{'>'}` or `&gt;`?

working.tsx (sort-of)

export interface GenericComponentProps<T> {
  prop: T
}

export const GenericComponent = <T extends unknown = unknown>(props: GenericComponentProps<T>) => (
  <div>hello</div>
);
// Error: 5:34  error  Constraining the generic type `T` to `unknown` does nothing and is unnecessary  @typescript-eslint/no-unnecessary-type-constraint

Please consider creating an isolated reproduction repo to make it easy for the volunteer maintainers debug your issue. –>

{
  "root": true,
  "parser": "@typescript-eslint/parser",
  "plugins": [
    "@typescript-eslint"
  ],
  "rules": {
    "@typescript-eslint/no-unnecessary-type-constraint": "error"
  }
}

Expected Result

Typescript eslint should not issue a warning as the suggested code (as below)

Actual Result

The code provided in working.tsx is required due to how generics are parsed in Typescript/React, one cannot simply write <T>.

Additional Info The attempted fix is invalid code:

export const GenericComponent = <T, = unknown>(props: GenericComponentProps<T>) => (
  <div>hello</div>
);

Versions (Tested both)

package version
@typescript-eslint/eslint-plugin 5.2.0
@typescript-eslint/parser 5.2.0
TypeScript 4.4.4
ESLint 8.1.0
node 14.16.0
package version
@typescript-eslint/eslint-plugin 4.33.1-alpha.15
@typescript-eslint/parser 4.33.1-alpha.15
TypeScript 4.4.4
ESLint 7.32.0
node 14.16.0

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 12
  • Comments: 16 (9 by maintainers)

Commits related to this issue

Most upvoted comments

The fix is to update the rule to allow arrow functions to declare extends unknown if the generic also has a default set.

// Bad
function foo<T extends unknown>() {}
function bar<T extends unknown = unknown>() {}

// Bad
const x = function <T extends unknown>() {};
const y = function <T extends unknown = unknown>() {};

// Bad
const z = <T extends unknown>() => {};

// Good
const a = <T,>() => {};
const b = <T extends unknown = unknown>() => {};

@Josh-Cena no, removing a fixer is just a patch bump as it’s a bug fix.

delete is probably the wrong word for the other issue. It’s true that we need to remove the autofixer because it can break the build. But there’s no reason it could be a suggestion fixer instead. Suggestion fixers are allowed to break the build because they can only be applied manually via the IDE.

So we should still fix the logic because the logic can still be used.

The fixer also fixes

const GenericComponent = <T extends unknown, U extends unknown>(props: T) => {};

to

const GenericComponent = <T,, U,>(props: T) => {};

We should only add the trailing comma if there’s only one generic argument and there’s no default