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 `>`?
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
- fix(eslint-plugin): allow unnecessary constraint in JSX arrow functions with default. Fixes #4062 — committed to graup/typescript-eslint by graup 5 years ago
- Ignore unresolvable eslint issues Not necessarily unresolvable but not worth the effort of resolving given the time constraint. The object destructuring one would make the code less readable so is n... — committed to js-jslog/harpguru-lift-shift-sdk-47 by deleted user 4 years ago
The fix is to update the rule to allow arrow functions to declare
extends unknownif the generic also has a default set.@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
to
We should only add the trailing comma if there’s only one generic argument and there’s no default