-
-
Notifications
You must be signed in to change notification settings - Fork 2.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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
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 on https://opencollective.com/typescript-eslint. As a thank you, your profile/company logo will be added to our main README which receives thousands of unique visitors per day. |
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.
interface Foo {
a: number;
}
interface Bar {
a: string | null;
}
declare const x: 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 the number
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:
interface Foo {
a: number;
}
interface Bar {
a: string | null;
}
declare const x: {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!
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.