Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2.8k
fix: [no-unnecessary-condition] use assignability APIs in no-unnecessary-condition (POC)#10378
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
base:main
Are you sure you want to change the base?
Uh oh!
There was an error while loading.Please reload this page.
Conversation
Thanks for the PR,@kirkwaiblinger! typescript-eslint is a 100% community driven project, and we are incredibly grateful that you are contributing to that community. The core maintainers work on this in their personal time, so please understand that it may not be possible for them to review your work immediately. Thanks again! 🙏Please, if you or your company is finding typescript-eslint valuable, help us sustain the project by sponsoring it transparently onhttps://opencollective.com/typescript-eslint. |
netlifybot commentedNov 23, 2024 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
✅ Deploy Preview fortypescript-eslint ready!
To edit notification comments on pull requests, go to yourNetlify site configuration. |
nx-cloudbot commentedNov 23, 2024 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
☁️ Nx Cloud ReportCI is running/has finished running commands for commit0b8d6b8. As they complete they will appear below. Click to see the status, the terminal output, and the build insights. 📂 See all runs for this CI Pipeline Execution ✅ Successfully ran 2 targetsSent with 💌 fromNxCloud. |
codecovbot commentedNov 23, 2024 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@## main #10378 +/- ##==========================================- Coverage 86.69% 86.67% -0.03%========================================== Files 434 434 Lines 15227 15226 -1 Branches 4445 4442 -3 ==========================================- Hits 13201 13197 -4- Misses 1673 1675 +2- Partials 353 354 +1
Flags with carried forward coverage won't be shown.Click here to find out more.
|
JoshuaKGoldberg left a comment• edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
I like this direction! Nicely done 🙂
@@ -107,7 +107,7 @@ function assert(condition: unknown): asserts condition { | |||
assert(false); // Unnecessary; condition is always falsy. | |||
const neverNull = {}; | |||
const neverNull = { someProperty: 'someValue'}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
That's due to TS's (questionable?) decision to give
neverNull
type{}
instead of typeobject
, even when initializing aconst
variable.
😩 this again... is there a TypeScript issue filed that we can reference? It feels like a bug to me.
I also don't mind it very much. It's not often that folks use{}
. This docs change kind of makes it more realistic to me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
😩 this again... is there a TypeScript issue filed that we can reference? It feels like a bug to me.
That's a good question. I'll take an action item to investigate this.
I also don't mind it very much. It's not often that folks use
{}
. This docs change kind of makes it more realistic to me.
Agreed that the docs change is probably an improvement. But, the false negative here makes the rule a little less understandable to me, so I'd lean on the side of wanting it to be correct if we can get it to be.
kirkwaiblingerNov 24, 2024 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Oh, dear, the moment I start playing around with this, I immediately find infuriating behavior around{}
....
functiontakesObject(x:object){if(x){console.log('should always be true')}else{console.error('this should be unreachable');}}consto:{}=0;// no error here!?!?!?!?takesObject(o);
I hate the{}
type so much.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Regardingconst o = {}; // has type {}
, started discord conversation athttps://discord.com/channels/508357248330760243/508357638677856287/1310419206260654081.
Regarding{}
->object
assignments, filedmicrosoft/TypeScript#60582... and immediately learned this is a duplicate ofmicrosoft/TypeScript#56205, in which this behavior is deemed intentional 🫤.
Reminder to self: I'm pretty sure we'll have some good info to add to the motivation forhttps://typescript-eslint.io/rules/no-empty-object-type/ as an outcome of these discussions 😆
Uh oh!
There was an error while loading.Please reload this page.
PR Checklist
Overview
This fundamentally changes the truthiness/falsiness algorithm used in no-unnecessary-condition. Up until now, the rule has checked various heuristics for possible falsiness: essentially whether the type is a known falsy literal and whether the type has TS's (inconsistent) PossiblyFalsy flag set. The proposed change is to use the assignability API to simply check whether one of JS's few falsy types is assignable to the type being investigated.
This has a few interesting consequences that improve correctness....
Annoyingly, this also has the drawback that the following no longer flags.
That's due to TS's (questionable?) decision to give
neverNull
type{}
instead of typeobject
, even when initializing aconst
variable.The following do still flag
If that specific case is something we want to preserve, wecould do so with scope analysis.