Skip to content

Fix generic mapped type relationships #19564

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
merged 3 commits into from
Oct 30, 2017

Conversation

ahejlsberg
Copy link
Member

Fixes #18201.

@ahejlsberg ahejlsberg requested a review from mhegazy October 30, 2017 00:00
@ahejlsberg
Copy link
Member Author

ahejlsberg commented Oct 30, 2017

@mhegazy This fix makes T related to Partial<T> for any generic T (as opposed to just a type parameter).

@@ -9454,32 +9454,30 @@ namespace ts {
}
}
}
else if (isGenericMappedType(target) && !isGenericMappedType(source) && getConstraintTypeFromMappedType(<MappedType>target) === getIndexType(source)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a branch lower down that checks whether target is a generic mapped type. Can this be performed there?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No because that particular section of code is only entered if source is an object or intersection type.

@ahejlsberg ahejlsberg merged commit 7f4a132 into master Oct 30, 2017
@ahejlsberg ahejlsberg deleted the fixGenericMappedTypeRelationships branch October 30, 2017 19:42
sandersn added a commit that referenced this pull request Mar 15, 2018
Recursive mapped types usually lead to the error "excess stack depth
comparing types" because of a new type relation rule added in #19564
which says that "A source type T is related to a target type { [P in
keyof T]: X } if T[P] is related to X".

Unfortunately, with self-recursive mapped types like

```ts
D<T> = { [P in keyof T]: D<T[P]> }
```
we get infinite recursion when trying to assign a type
parameter T to D<T>, as T[P] is compared to D<T[P]>, T[P][P] is compared
to D<T[P][P]>, and so on.

We can avoid many of these infinite recursions by replacing occurrences
of D in the template type with its type argument. This works because
mapped types will completely cover the tree, so checking assignability
of the top level implies that checking of lower level would succeed,
even if there are infinitely many levels.

For example:

```ts
D<T> = { [P in keyof T]: D<T[P]> | undefined }
<T>(t: T, dt: D<T>) => { dt = t }
```

would previously check that `T[P]` is assignable to `D<T[P]> |
undefined`. Now, after replacement, it checks that `T[P]` is assignable
to `T[P] | undefined`.

This implementation suffers from 3 limitations:
1. I use aliasSymbol to detect whether a type reference is a
self-recursive one. This only works when the mapped type is at the top
level of a type alias.
2. Not all instances of D<T> are replaced with T, just those in
intersections and unions. I think this covers almost all uses.
3. This doesn't fix #21048, which tries to assign an "off-by-one"
partial-deep type to itself.

Mostly fixes #21592. One repro there has a type alias to a union, and a
mapped type is a member of the union. But this can be split into two
aliases:

```ts
type SafeAnyMap<T> = { [K in keyof T]?: SafeAny<T[K] };
type SafeAny<T> = SafeAnyMap<T> | boolean | string | symbol | number | null | undefined;
```
@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants