-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Bug: [no-unnecessary-condition] if (new.target)
shouldn't trigger the rule.
#11221
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
Comments
Hi @miguel-leon ! Could you explain why From a glance, I noted:
WDYT? |
Hi @kirkwaiblinger,
From the docs:
Looks like an oversight on typescript's part not to have it be a union with
Are you saying that all projects that use the linter must have those options? |
This seems like a TypeScript bug. It should not allow code like this: function Fn() {
console.log(new.target.name);
} microsoft/TypeScript#48344 is related, but I didn't find anything about |
Not exactly, but it informs our willingness to special-case things that can already be handled natively by TS...
Heh, I'll admit that I've never seen code like that and I don't have an opinion on it yet 😅. In any case, on a re-read I agree with your and Josh's observation that the TS type appears to be incorrect, so addressing that is the best way to proceed. |
Since that TS bug is causing typescript-eslint's rule to error in a place not expected by the user, and you consider typescript-eslint needs not to account for this; would you consider the issue in TS be filed by a typescript-eslint representative? plus it'll surely have more weight than if I did it. |
I'm pretty sure in a future "major" release they'll be changing the defaults for the compiler options to make things better by default.
We don't seek to cover all cases with rules. There are bound to be false positives in some edge cases that we don't or cannot handle. This rule is pretty straightforward in its design - it is powered by types. If the types are wrong then the rule is wrong - simple as that. The only special casing it has is around records and arrays where TS internally doesn't model the undefined properly.
Yes - I personally would not consider it well-formed code. In modern JS and TS having functions that may be both called as a function or called as a constructor is widely considered unnecessarily permissive. Most people seek to reduce API surface area and reduce variance in how one consumes things to reduce choice and complexity where it doesn't add value. There's no reason to not just be one or the other either in TS given that TS can easily downlevel class syntax if you're running in an env that doesn't support classes. |
I'd recommend you give it a try! The TS folks are pretty egalitarian 🙂. I'd recommend linking this issue as relevant context, though, of course! |
I appreciate your opinion, though you either contradicted yourself, or I didn't understand what you meant, or something else.
If there are two functionalities, I'd understand as less API surface having a single identifier rather than two. And if the functionalities are usable only in distinct places (for example as a decorator and as a base class), then there's no variance increase either. I was trying to reason about it and return the suggestion. I know they're egalitarian and I've reported plenty there. I feel it's not up to me to do it in this case, so I won't do it. Even if the reason is external to this package; as a user, typescript-eslint is the package that is producing me an unexpected error; so I reported it here. To me, it seems reasonable that this package either escalates the bug to its dependency, or disables the rule for the unreliable case |
Well it depends really. If your intent is that Though if that's your intent I'd further lean into this being not-well-formed TS code. Making a function work as a class or as a function historically was done in JS as a way to be forgiving to people -- to allow people to "forget |
As explained -- this rule is powered by the types in the code. We suggest that users raise an issue with the owners of the types to drive improvement and change there. We, the maintainers of typescript-eslint, are volunteers and so our time + bandwidth is limited. Getting these types corrected is not something that is important to us as we do not use If this is important to you, please raise an issue with the owners of the types to drive the change to correct the types so that they correctly reflect reality. Footnotes
|
Well, having Also, I think the definition for "not-well-formed" has no relation to "established practices", so you might need to look that up. Ultimately, all your negative connotations about certain "ways to code" are simply personal opinion and debatable. I would be more humble about them, but I appreciate them nonetheless.
To be clear, I don't really care if the issue gets fixed or doesn't. At the end of the day, this is your project, you decide who is allowed to add changes, and you decide how complete and accurate you want the project to be. As I stated earlier, I report this here because, for the reasons I explained, this is the appropriate place to do so. Afterall, typescript-eslint is the one erroring on something that doesn't "reflect reality". Do with this issue what you will. Specially if the fix takes too much time. Although I'd argue that writing all these justifications would take longer. |
Before You File a Bug Report Please Confirm You Have Done The Following...
Playground Link
https://typescript-eslint.io/play/#ts=5.8.2&fileType=.ts&code=GYVwdgxgLglg9mABAMTACgJSIN4FgBQiiMwiaYApgO4B0UAhgE4DmFUWehRiBRAvgQH4gA&eslintrc=N4KABGBEBOCuA2BTAzpAXGUEKQAIBcBPABxQGNoBLY-AWhXkoDt8B6Jge1tiacTJTIAhtEK0yHJgBNK%2BSpPRRE0aB2iRwYAL4gtQA&tsconfig=N4KABGBEDGD2C2AHAlgGwKYCcDyiAuysAdgM6QBcYoEEkJemy0eAcgK6qoDCAFutAGsylBm3TgwAXxCSgA&tokens=false
Repro Code
ESLint Config
tsconfig
Expected Result
No error.
Actual Result
Errors.
Additional Info
Additional info: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/new.target
The text was updated successfully, but these errors were encountered: