-
Notifications
You must be signed in to change notification settings - Fork 12.9k
fix narrowTypeByDiscriminant for non null expression access #52136
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
@typescript-bot perf test this faster |
Heya @gabritto, I've started to run the diff-based user code test suite on this PR at 366f92a. You can monitor the build here. Update: The results are in! |
Heya @gabritto, I've started to run the abridged perf test suite on this PR at 366f92a. You can monitor the build here. Update: The results are in! |
Heya @gabritto, I've started to run the diff-based top-repos suite on this PR at 366f92a. You can monitor the build here. Update: The results are in! |
@gabritto Here are the results of running the user test suite comparing Everything looks good! |
@gabritto Here they are:Comparison Report - main..52136
System
Hosts
Scenarios
Developer Information: |
@gabritto Here are the results of running the top-repos suite comparing Everything looks good! |
@@ -26299,7 +26300,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { | |||
if (propName === undefined) { | |||
return type; | |||
} | |||
const removeNullable = strictNullChecks && isOptionalChain(access) && maybeTypeOfKind(type, TypeFlags.Nullable); | |||
const removeNullable = strictNullChecks && (isOptionalChain(access) || isNonNullAccess(access)) && maybeTypeOfKind(type, TypeFlags.Nullable); |
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.
Here we remove null
or undefined
from the type, but then a few lines below we add back only an undefined
. That reflects what happens in an optional chain, but the !
operator never changes one to the other. It may not matter, but doesn't quite seem right.
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 see, I had missed that. So I guess we shouldn't call getOptionalType
below if it's a !
operator, since !
actually gets rid of undefined
and null
, unlike an optional chain.
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.
@ahejlsberg I pushed the above change, let me know if it's not right or if there's something else missing.
Fixes #52118.