Skip to content
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

Closed
wants to merge 2 commits into from

Conversation

weswigham
Copy link
Member

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} and K, and produce a new indexed access - that becomes ({} & {})[string | number | symbol], which then becomes the error 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 to T[K] & Foo (since K is keyof T) whose constraint is simply Foo.

Fixes #25181

@weswigham weswigham requested review from ahejlsberg and mhegazy July 6, 2018 23:56
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
Copy link
Member

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
Copy link
Member

@DanielRosenwasser DanielRosenwasser Jul 7, 2018

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?

Copy link
Member Author

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.

Copy link
Member

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)) {
Copy link
Member

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?

@weswigham
Copy link
Member Author

Superseded by #26281, which is much more elegant, IMO.

@weswigham weswigham closed this Aug 8, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants