-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Add transformation to indexing intersection of homomorphic mapepd type and generic parameter #25490
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 |
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.
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 |
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.
Why not directly overwrite the original location?
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.
... fair point. I was in a splicy mood, I suppose.
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.
🔥 🔥 🔥 🔥
// 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)) { |
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.
Could you turn this into a findIndex
and then avoid the inner loop?
Superseded by #26281, which is much more elegant, IMO. |
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 theerror
type.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
(sinceK
iskeyof T
) whose constraint is simplyFoo
.Fixes #25181