Add transformation to indexing intersection of homomorphic mapepd type and generic parameter#25490
Closed
weswigham wants to merge 2 commits intomicrosoft:masterfrom
Closed
Add transformation to indexing intersection of homomorphic mapepd type and generic parameter#25490weswigham wants to merge 2 commits intomicrosoft:masterfrom
weswigham wants to merge 2 commits intomicrosoft:masterfrom
Conversation
…e and generic parameter
| newMappedType.typeParameter = (t as MappedType).typeParameter; | ||
| newMappedType.constraintType = (t as MappedType).constraintType; | ||
| const newSet = (<IntersectionType>objectType).types.slice(); | ||
| newSet.splice(matchIndex!, 1); // Remove the generic |
Member
There was a problem hiding this comment.
orderedRemoveItemAt (or unorderedRemoveItemAt if it doesn't matter)
| newMappedType.constraintType = (t as MappedType).constraintType; | ||
| const newSet = (<IntersectionType>objectType).types.slice(); | ||
| newSet.splice(matchIndex!, 1); // Remove the generic | ||
| newSet.splice(newSet.indexOf(t), 1, newMappedType); // Remove the original mapped type |
Member
There was a problem hiding this comment.
Why not directly overwrite the original location?
Member
Author
There was a problem hiding this comment.
... fair point. I was in a splicy mood, I suppose.
| // If we don't do such a transformation now, due to how we reason over intersections, we will never come to the | ||
| // realization that the member we're trying to pluck out is influenced by both `T[P]` _and_ the mapped type template | ||
| const isInSet = isMappedTypeOverKeyofGenericInSet((<IntersectionType>objectType).types); | ||
| if (some((<IntersectionType>objectType).types, isInSet)) { |
Member
There was a problem hiding this comment.
Could you turn this into a findIndex and then avoid the inner loop?
Member
Author
|
Superseded by #26281, which is much more elegant, IMO. |
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
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
In
getSimplifiedType, adds a branch which transforms(T & {[P in keyof T]: Foo})[K](which we can't reason about, thanks to how we combine the intersection before looking for members on its apparent type) into{[P in keyof T]: T[P] & Foo}[K](which we can reason about).Specifically, in the first case, when calculating its constraint, we see that it's an indexed access, attempt to simplify it (and it doesn't), then we get the base constraints of
T & {[P in keyof T]: Foo}andK, and produce a new indexed access - that becomes({} & {})[string | number | symbol], which then becomes theerrortype.Now, when we attempt to simplify it, we apply the above mapping, and the index type
{[P in keyof T]: T[P] & Foo}[K]in the provided sample simplifies toT[K] & Foo(sinceKiskeyof T) whose constraint is simplyFoo.Fixes #25181