Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 13.7k
Improve narrowing logic for instanceof, type predicate functions, and assertion functions#49625
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
Merged
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b779e3b
Improve narrowing logic for instanceof, type predicates, and assertions
ahejlsberg cbd3f3d
Accept new baselines
ahejlsberg fc81aff
Add tests
ahejlsberg d6d1533
Tweak algorithm
ahejlsberg 37bfa91
Accept new baselines
ahejlsberg e85971c
Optimize for discriminated unions
ahejlsberg 960ad11
Merge branch 'main' into fix31156
ahejlsberg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -25388,23 +25388,32 @@ m2: ${(this.mapper2 as unknown as DebugTypeMapper).__debugToString().split("\n") | ||
| if (!assumeTrue) { | ||
| return filterType(type, t => !isRelated(t, candidate)); | ||
| } | ||
| // If the current type is a union type, remove all constituents that couldn't be instances of | ||
| // the candidate type. If one or more constituents remain, return a union of those. | ||
| if (type.flags & TypeFlags.Union) { | ||
| const assignableType = filterType(type, t => isRelated(t, candidate)); | ||
| if (!(assignableType.flags & TypeFlags.Never)) { | ||
| return assignableType; | ||
| } | ||
| } | ||
| // If the candidate type is a subtype of the target type, narrow to the candidate type. | ||
| // Otherwise, if the target type is assignable to the candidate type, keep the target type. | ||
| // Otherwise, if the candidate type is assignable to the target type, narrow to the candidate | ||
| // type. Otherwise, the types are completely unrelated, so narrow to an intersection of the | ||
| // two types. | ||
| return isTypeSubtypeOf(candidate, type) ? candidate : | ||
| isTypeAssignableTo(type, candidate) ? type : | ||
| isTypeAssignableTo(candidate, type) ? candidate : | ||
| getIntersectionType([type, candidate]); | ||
| if (type.flags & TypeFlags.AnyOrUnknown) { | ||
| return candidate; | ||
| } | ||
| // We first attempt to filter the current type, narrowing constituents as appropriate and removing | ||
| // constituents that are unrelated to the candidate. | ||
| const keyPropertyName = type.flags & TypeFlags.Union ? getKeyPropertyName(type as UnionType) : undefined; | ||
| const narrowedType = mapType(candidate, c => { | ||
| // If a discriminant property is available, use that to reduce the type. | ||
| const discriminant = keyPropertyName && getTypeOfPropertyOfType(c, keyPropertyName); | ||
| const matching = discriminant && getConstituentTypeForKeyType(type as UnionType, discriminant); | ||
| // For each constituent t in the current type, if t and and c are directly related, pick the most | ||
| // specific of the two. | ||
| const directlyRelated = mapType(matching || type, t => isRelated(t, c) ? t : isRelated(c, t) ? c : neverType); | ||
| // If no constituents are directly related, create intersections for any generic constituents that | ||
DanielRosenwasser marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| // are related by constraint. | ||
| return directlyRelated.flags & TypeFlags.Never ? | ||
| mapType(type, t => maybeTypeOfKind(t, TypeFlags.Instantiable) && isRelated(c, getBaseConstraintOfType(t) || unknownType) ? getIntersectionType([t, c]) : neverType) : | ||
| directlyRelated; | ||
| }); | ||
| // If filtering produced a non-empty type, return that. Otherwise, pick the most specific of the two | ||
| // based on assignability, or as a last resort produce an intersection. | ||
| return !(narrowedType.flags & TypeFlags.Never) ? narrowedType : | ||
| isTypeSubtypeOf(candidate, type) ? candidate : | ||
| isTypeAssignableTo(type, candidate) ? type : | ||
| isTypeAssignableTo(candidate, type) ? candidate : | ||
| getIntersectionType([type, candidate]); | ||
| } | ||
| function narrowTypeByCallExpression(type: Type, callExpression: CallExpression, assumeTrue: boolean): Type { | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.