-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Consider index signatures as optional properties in contextual type discrimination #54596
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
src/compiler/checker.ts
Outdated
@@ -22719,6 +22719,15 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { | |||
} | |||
const filtered = contains(include, Ternary.False) ? getUnionType(types.filter((_, i) => include[i])) : target; | |||
return filtered.flags & TypeFlags.Never ? target : filtered; | |||
|
|||
function getTypeOfPropertyOrOptionalIndexSignature(type: Type, name: __String): Type | undefined { |
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 feel like this function might already exist, but I couldn't find it.
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.
There's getTypeOfPropertyOrIndexSignature
(used by narrowTypeByDiscriminant
!). Consider replacing that function and making it take a parameter on whether to add optionality for index signatures.
@typescript-bot test this |
Heya @gabritto, I've started to run the parallelized Definitely Typed test suite on this PR at c66c0d1. You can monitor the build here. Update: The results are in! |
Heya @gabritto, I've started to run the diff-based user code test suite on this PR at c66c0d1. You can monitor the build here. Update: The results are in! |
Heya @gabritto, I've started to run the perf test suite on this PR at c66c0d1. 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 c66c0d1. You can monitor the build here. Update: The results are in! |
@gabritto Here are the results of running the user test suite comparing There were infrastructure failures potentially unrelated to your change:
Otherwise... Something interesting changed - please have a look. Details
|
@gabritto Here they are:
CompilerComparison Report - main..54596
System
Hosts
Scenarios
TSServerComparison Report - main..54596
System
Hosts
Scenarios
StartupComparison Report - main..54596
System
Hosts
Scenarios
Developer Information: |
@gabritto Here are the results of running the top-repos suite comparing Everything looks good! |
Hey @gabritto, the results of running the DT tests are ready. |
This didn't land in 5.1.6 right? When is it expected to be released? |
This is in the 5.2 beta (also noted in the issue's milestone). |
Fixes #54498.
The fix is suggested and described by @nebkat in this comment: #54498 (comment).
When we try to discriminate a contextual type, we consider the omission of a property to be a factor to discriminate in
discriminateTypeByDiscriminableItems
's implementation, but then during that process, when looking up a property in one of the candidate types, we would not consider matching index signatures and therefore we would discard the type with the index signature. Now, when looking up a property in one of the candidate types, we also consider matching on index signatures.