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(eslint-plugin): [no-unnecessary-condition] improve optional chain handling 2 - electric boogaloo#2138
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
Uh oh!
There was an error while loading.Please reload this page.
Conversation
Thanks for the PR,@yeonjuan! 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. As a thank you, your profile/company logo will be added to our main README which receives thousands of unique visitorsper day. |
codecovbot commentedMay 31, 2020 • 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 Report
@@ Coverage Diff @@## master #2138 +/- ##==========================================+ Coverage 93.54% 93.56% +0.01%========================================== Files 171 171 Lines 7798 7811 +13 Branches 2225 2229 +4 ==========================================+ Hits 7295 7308 +13 Misses 244 244 Partials 259 259
|
const ownPropertyType = prevType.types | ||
.map(type => checker.getTypeOfPropertyOfType(type, property.name)) | ||
.find(t => t); |
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.
Mapping over the types in the union isn't quite right, and will cause issues in the following case. The checker API is smart enough to analyse unions to get the type of a property on that union.
interfaceFoo{a:number;}interfaceBar{a:string|null;}declareconstx:Foo|Bar;
# type = type of x;$ type.types.map(t => checker.typeToString(checker.getTypeOfPropertyOfType(t,'a')))> ["number","string | null"]
Eg with your current logic, this case will just check thenumber
case, and will thus false-positive on the property.
If you instead just ask for the property on the union type itself:
# type = type of x;$ checker.typeToString(checker.getTypeOfPropertyOfType(type,'a'))>"string | number | null"
It will work as expected. However if for some reason the base type is nullish, you need to also scrub the null from the union first:
interfaceFoo{a:number;}interfaceBar{a:string|null;}declareconstx:{t:Foo|Bar}|null;x?.t.a;
# type = type of x?.t;$ nonNullType = checker.getNonNullableType(type)$ checker.typeToString(checker.getTypeOfPropertyOfType(nonNullType,'a'))>"string | number | null"
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.
@bradzacher
Thanks for the review. I'll change it soon :)
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.
LGTM - thanks for your work!
Uh oh!
There was an error while loading.Please reload this page.
Fixes#1977
#1977 occurs when member expression's nullable type is originated from the previous node.
So, I changed it to check whether a member expression's nullable type is origin from its previous.