-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bug: [no-unnecessary-type-constraint] False positive for generics on arrow functions in .mts files #5449
Comments
Somehow, I had a feeling this line of code would come back to haunt us 😄: typescript-eslint/packages/eslint-plugin/src/rules/no-unnecessary-type-constraint.ts Line 62 in 48c4ed6
Agreed, this is a bug. The rule should understand |
We should probably somehow export the utils here to reuse them so we can share logic across packages? typescript-eslint/packages/typescript-estree/src/create-program/getScriptKind.ts Lines 4 to 48 in 399b3b1
|
TS7060:
A trailing comma should also be fine. Like so: const func = <T,>(name: T) => {
console.log(name);
} Though if you're using a formatter, it might end up removing the trailing comma. |
With any issue opened in this project — it either has visible progress in the form of an attached PR, or it has no progress. We are a community-run project. The volunteer maintainers spend most of their time triaging issues and reviewing PRs. This means that most issues will not progress unless a member of the community steps up and champions it. If this issue is important to you — consider being that champion. If not — please just subscribe to the issue and wait patiently. |
@bradzacher This is all very well, but the reality is that these packages require significant amounts of context before approaching being the "champion". This is an issue that's cropped up for me as well, and I've spent a fair amount of my time trying to understand why this issue even exists in the first place. I'm still not anywhere close to understanding what needs to change in order to fix. Any help would be appreciated? I'm more than happy to open a pr. If an issue is labelled |
@WoodyWoodsta Josh was pretty succinct in his analysis of the problem in the very first comment on this thread
My second comment in this thread suggested a utility we could expose from another one of our packages to use instead of the current and incorrect check. So the PR to fix this would be
|
@bradzacher Appreciate the response - Josh was very succinct about the symptom, but to create the link between that and the actual problem wasn't as easy. Instead the member analysis was "this came back to bite us" and "We should probably somehow export the utils here..." which doesn't help anyone understand what you've crucially highlighted which is that somehow |
To clarify - in JSX files the single generic argument arrow function syntax ( Hence the correct fix in a JSX file is from When TS introduced So hence there's a problem in the rule. The rule was built in a time when the only JSX file was So the options to solve this are two fold:
|
The background of JSX tags and generic argument syntax isn't new to me, that makes complete sense. To clarify, is
But I think I have enough understanding for a fix. Thank you! |
@WoodyWoodsta the source of the issue is specifically the line of code Josh linked in the initial comment: typescript-eslint/packages/eslint-plugin/src/rules/no-unnecessary-type-constraint.ts Line 63 in 8ee0cbb
|
Sorry @bradzacher, my snippet I linked was from the jest rule tester, not the actual plugin code. Can't digest an entire monorepo in one day. Let's continue the discussion in #6795 |
I'm seeing this issue on
Are these related, or should I open a separate issue? Versions:"@typescript-eslint/eslint-plugin": "^5.61.0",
"@typescript-eslint/parser": "^5.61.0",
"eslint": "^8.44.0", Rules Config:"@typescript-eslint/comma-dangle": ["error", "always-multiline"],
"@typescript-eslint/no-unnecessary-type-constraint": "error",
"comma-spacing": "error", |
Those are separate issues. |
Before You File a Bug Report Please Confirm You Have Done The Following...
Issue Description
When using the new (as of TS 4.7)
.mts
file extension theconst foo = <T>() => ...
syntax for generic arguments is reserved similar to JSX files. The suggestion from tsc recommends defining an explicit constraint:Since my function doesn't need or nor do I want a type constraint, but I need one for the syntax, I reached for the noop constraint of
extends any
, but then I was violating theno-unnecessary-type-constraint
rule.In this case, with the
.mts
extension and it's limited syntax I think it's unfair to say it "doesn't do anything and is unnecessary".Reproduction Repository Link
https://github.com/spalger/reproduce-issues/tree/ts-eslint-no-unnecessary-type-constraint
Repro Steps
clone the repo with
--branch
git clone git@github.com:spalger/reproduce-issues.git --branch ts-eslint-no-unnecessary-type-constraint
npm i
npm run lint
Versions
@typescript-eslint/eslint-plugin
5.33.0
@typescript-eslint/parser
5.33.0
TypeScript
4.7.4
ESLint
8.21.0
node
18.7.0
The text was updated successfully, but these errors were encountered: