Skip to content

Fix relation between generic mapped types and types with index signatures #38761

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 2 commits into from
May 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17288,9 +17288,10 @@ namespace ts {
return Ternary.True;
}
if (isGenericMappedType(source)) {
// A generic mapped type { [P in K]: T } is related to an index signature { [x: string]: U }
// if T is related to U.
return kind === IndexKind.String ? isRelatedTo(getTemplateTypeFromMappedType(source), targetType, reportErrors) : Ternary.False;
// A generic mapped type { [P in K]: T } is related to a type with an index signature
// { [x: string]: U }, and optionally with an index signature { [x: number]: V },
// if T is related to U and V.
return getIndexTypeOfType(target, IndexKind.String) ? isRelatedTo(getTemplateTypeFromMappedType(source), targetType, reportErrors) : Ternary.False;
}
const indexType = getIndexTypeOfType(source, kind) || kind === IndexKind.Number && getIndexTypeOfType(source, IndexKind.String);
if (indexType) {
Expand Down
36 changes: 36 additions & 0 deletions tests/baselines/reference/mappedTypeAndIndexSignatureRelation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//// [mappedTypeAndIndexSignatureRelation.ts]
type Same<T> = { [P in keyof T]: T[P] };

type T1<T extends Record<PropertyKey, number>> = T;
type T2<U extends Record<PropertyKey, number>> = T1<Same<U>>;

// Repro from #38235

type Foo<IdentifierT extends Record<PropertyKey, PropertyKey>> =
IdentifierT
;

type Bar<IdentifierT extends Record<PropertyKey, PropertyKey>, T> =
{
[k in keyof T] : Foo<IdentifierT & { k : k }>
}
;

type Merge2<T> = { [k in keyof T] : T[k] }
type Bar2<IdentifierT extends Record<PropertyKey, PropertyKey>, T> =
{
[k in keyof T]: Foo<Merge2<IdentifierT & { k: k }>>
}
;

type Identity<T> = T;
type Merge3<T> = Identity<{ [k in keyof T] : T[k] }>
type Bar3<IdentifierT extends Record<PropertyKey, PropertyKey>, T> =
{
[k in keyof T]: Foo<Merge3<IdentifierT & { k: k }>>
}
;


//// [mappedTypeAndIndexSignatureRelation.js]
"use strict";
117 changes: 117 additions & 0 deletions tests/baselines/reference/mappedTypeAndIndexSignatureRelation.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
=== tests/cases/compiler/mappedTypeAndIndexSignatureRelation.ts ===
type Same<T> = { [P in keyof T]: T[P] };
>Same : Symbol(Same, Decl(mappedTypeAndIndexSignatureRelation.ts, 0, 0))
>T : Symbol(T, Decl(mappedTypeAndIndexSignatureRelation.ts, 0, 10))
>P : Symbol(P, Decl(mappedTypeAndIndexSignatureRelation.ts, 0, 18))
>T : Symbol(T, Decl(mappedTypeAndIndexSignatureRelation.ts, 0, 10))
>T : Symbol(T, Decl(mappedTypeAndIndexSignatureRelation.ts, 0, 10))
>P : Symbol(P, Decl(mappedTypeAndIndexSignatureRelation.ts, 0, 18))

type T1<T extends Record<PropertyKey, number>> = T;
>T1 : Symbol(T1, Decl(mappedTypeAndIndexSignatureRelation.ts, 0, 40))
>T : Symbol(T, Decl(mappedTypeAndIndexSignatureRelation.ts, 2, 8))
>Record : Symbol(Record, Decl(lib.es5.d.ts, --, --))
>PropertyKey : Symbol(PropertyKey, Decl(lib.es5.d.ts, --, --))
>T : Symbol(T, Decl(mappedTypeAndIndexSignatureRelation.ts, 2, 8))

type T2<U extends Record<PropertyKey, number>> = T1<Same<U>>;
>T2 : Symbol(T2, Decl(mappedTypeAndIndexSignatureRelation.ts, 2, 51))
>U : Symbol(U, Decl(mappedTypeAndIndexSignatureRelation.ts, 3, 8))
>Record : Symbol(Record, Decl(lib.es5.d.ts, --, --))
>PropertyKey : Symbol(PropertyKey, Decl(lib.es5.d.ts, --, --))
>T1 : Symbol(T1, Decl(mappedTypeAndIndexSignatureRelation.ts, 0, 40))
>Same : Symbol(Same, Decl(mappedTypeAndIndexSignatureRelation.ts, 0, 0))
>U : Symbol(U, Decl(mappedTypeAndIndexSignatureRelation.ts, 3, 8))

// Repro from #38235

type Foo<IdentifierT extends Record<PropertyKey, PropertyKey>> =
>Foo : Symbol(Foo, Decl(mappedTypeAndIndexSignatureRelation.ts, 3, 61))
>IdentifierT : Symbol(IdentifierT, Decl(mappedTypeAndIndexSignatureRelation.ts, 7, 9))
>Record : Symbol(Record, Decl(lib.es5.d.ts, --, --))
>PropertyKey : Symbol(PropertyKey, Decl(lib.es5.d.ts, --, --))
>PropertyKey : Symbol(PropertyKey, Decl(lib.es5.d.ts, --, --))

IdentifierT
>IdentifierT : Symbol(IdentifierT, Decl(mappedTypeAndIndexSignatureRelation.ts, 7, 9))

;

type Bar<IdentifierT extends Record<PropertyKey, PropertyKey>, T> =
>Bar : Symbol(Bar, Decl(mappedTypeAndIndexSignatureRelation.ts, 9, 1))
>IdentifierT : Symbol(IdentifierT, Decl(mappedTypeAndIndexSignatureRelation.ts, 11, 9))
>Record : Symbol(Record, Decl(lib.es5.d.ts, --, --))
>PropertyKey : Symbol(PropertyKey, Decl(lib.es5.d.ts, --, --))
>PropertyKey : Symbol(PropertyKey, Decl(lib.es5.d.ts, --, --))
>T : Symbol(T, Decl(mappedTypeAndIndexSignatureRelation.ts, 11, 62))
{
[k in keyof T] : Foo<IdentifierT & { k : k }>
>k : Symbol(k, Decl(mappedTypeAndIndexSignatureRelation.ts, 13, 9))
>T : Symbol(T, Decl(mappedTypeAndIndexSignatureRelation.ts, 11, 62))
>Foo : Symbol(Foo, Decl(mappedTypeAndIndexSignatureRelation.ts, 3, 61))
>IdentifierT : Symbol(IdentifierT, Decl(mappedTypeAndIndexSignatureRelation.ts, 11, 9))
>k : Symbol(k, Decl(mappedTypeAndIndexSignatureRelation.ts, 13, 44))
>k : Symbol(k, Decl(mappedTypeAndIndexSignatureRelation.ts, 13, 9))
}
;

type Merge2<T> = { [k in keyof T] : T[k] }
>Merge2 : Symbol(Merge2, Decl(mappedTypeAndIndexSignatureRelation.ts, 15, 1))
>T : Symbol(T, Decl(mappedTypeAndIndexSignatureRelation.ts, 17, 12))
>k : Symbol(k, Decl(mappedTypeAndIndexSignatureRelation.ts, 17, 20))
>T : Symbol(T, Decl(mappedTypeAndIndexSignatureRelation.ts, 17, 12))
>T : Symbol(T, Decl(mappedTypeAndIndexSignatureRelation.ts, 17, 12))
>k : Symbol(k, Decl(mappedTypeAndIndexSignatureRelation.ts, 17, 20))

type Bar2<IdentifierT extends Record<PropertyKey, PropertyKey>, T> =
>Bar2 : Symbol(Bar2, Decl(mappedTypeAndIndexSignatureRelation.ts, 17, 42))
>IdentifierT : Symbol(IdentifierT, Decl(mappedTypeAndIndexSignatureRelation.ts, 18, 10))
>Record : Symbol(Record, Decl(lib.es5.d.ts, --, --))
>PropertyKey : Symbol(PropertyKey, Decl(lib.es5.d.ts, --, --))
>PropertyKey : Symbol(PropertyKey, Decl(lib.es5.d.ts, --, --))
>T : Symbol(T, Decl(mappedTypeAndIndexSignatureRelation.ts, 18, 63))
{
[k in keyof T]: Foo<Merge2<IdentifierT & { k: k }>>
>k : Symbol(k, Decl(mappedTypeAndIndexSignatureRelation.ts, 20, 9))
>T : Symbol(T, Decl(mappedTypeAndIndexSignatureRelation.ts, 18, 63))
>Foo : Symbol(Foo, Decl(mappedTypeAndIndexSignatureRelation.ts, 3, 61))
>Merge2 : Symbol(Merge2, Decl(mappedTypeAndIndexSignatureRelation.ts, 15, 1))
>IdentifierT : Symbol(IdentifierT, Decl(mappedTypeAndIndexSignatureRelation.ts, 18, 10))
>k : Symbol(k, Decl(mappedTypeAndIndexSignatureRelation.ts, 20, 50))
>k : Symbol(k, Decl(mappedTypeAndIndexSignatureRelation.ts, 20, 9))
}
;

type Identity<T> = T;
>Identity : Symbol(Identity, Decl(mappedTypeAndIndexSignatureRelation.ts, 22, 1))
>T : Symbol(T, Decl(mappedTypeAndIndexSignatureRelation.ts, 24, 14))
>T : Symbol(T, Decl(mappedTypeAndIndexSignatureRelation.ts, 24, 14))

type Merge3<T> = Identity<{ [k in keyof T] : T[k] }>
>Merge3 : Symbol(Merge3, Decl(mappedTypeAndIndexSignatureRelation.ts, 24, 21))
>T : Symbol(T, Decl(mappedTypeAndIndexSignatureRelation.ts, 25, 12))
>Identity : Symbol(Identity, Decl(mappedTypeAndIndexSignatureRelation.ts, 22, 1))
>k : Symbol(k, Decl(mappedTypeAndIndexSignatureRelation.ts, 25, 29))
>T : Symbol(T, Decl(mappedTypeAndIndexSignatureRelation.ts, 25, 12))
>T : Symbol(T, Decl(mappedTypeAndIndexSignatureRelation.ts, 25, 12))
>k : Symbol(k, Decl(mappedTypeAndIndexSignatureRelation.ts, 25, 29))

type Bar3<IdentifierT extends Record<PropertyKey, PropertyKey>, T> =
>Bar3 : Symbol(Bar3, Decl(mappedTypeAndIndexSignatureRelation.ts, 25, 52))
>IdentifierT : Symbol(IdentifierT, Decl(mappedTypeAndIndexSignatureRelation.ts, 26, 10))
>Record : Symbol(Record, Decl(lib.es5.d.ts, --, --))
>PropertyKey : Symbol(PropertyKey, Decl(lib.es5.d.ts, --, --))
>PropertyKey : Symbol(PropertyKey, Decl(lib.es5.d.ts, --, --))
>T : Symbol(T, Decl(mappedTypeAndIndexSignatureRelation.ts, 26, 63))
{
[k in keyof T]: Foo<Merge3<IdentifierT & { k: k }>>
>k : Symbol(k, Decl(mappedTypeAndIndexSignatureRelation.ts, 28, 9))
>T : Symbol(T, Decl(mappedTypeAndIndexSignatureRelation.ts, 26, 63))
>Foo : Symbol(Foo, Decl(mappedTypeAndIndexSignatureRelation.ts, 3, 61))
>Merge3 : Symbol(Merge3, Decl(mappedTypeAndIndexSignatureRelation.ts, 24, 21))
>IdentifierT : Symbol(IdentifierT, Decl(mappedTypeAndIndexSignatureRelation.ts, 26, 10))
>k : Symbol(k, Decl(mappedTypeAndIndexSignatureRelation.ts, 28, 50))
>k : Symbol(k, Decl(mappedTypeAndIndexSignatureRelation.ts, 28, 9))
}
;

Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
=== tests/cases/compiler/mappedTypeAndIndexSignatureRelation.ts ===
type Same<T> = { [P in keyof T]: T[P] };
>Same : Same<T>

type T1<T extends Record<PropertyKey, number>> = T;
>T1 : T

type T2<U extends Record<PropertyKey, number>> = T1<Same<U>>;
>T2 : Same<U>

// Repro from #38235

type Foo<IdentifierT extends Record<PropertyKey, PropertyKey>> =
>Foo : IdentifierT

IdentifierT
;

type Bar<IdentifierT extends Record<PropertyKey, PropertyKey>, T> =
>Bar : Bar<IdentifierT, T>
{
[k in keyof T] : Foo<IdentifierT & { k : k }>
>k : k
}
;

type Merge2<T> = { [k in keyof T] : T[k] }
>Merge2 : Merge2<T>

type Bar2<IdentifierT extends Record<PropertyKey, PropertyKey>, T> =
>Bar2 : Bar2<IdentifierT, T>
{
[k in keyof T]: Foo<Merge2<IdentifierT & { k: k }>>
>k : k
}
;

type Identity<T> = T;
>Identity : T

type Merge3<T> = Identity<{ [k in keyof T] : T[k] }>
>Merge3 : { [k in keyof T]: T[k]; }

type Bar3<IdentifierT extends Record<PropertyKey, PropertyKey>, T> =
>Bar3 : Bar3<IdentifierT, T>
{
[k in keyof T]: Foo<Merge3<IdentifierT & { k: k }>>
>k : k
}
;

33 changes: 33 additions & 0 deletions tests/cases/compiler/mappedTypeAndIndexSignatureRelation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// @strict: true

type Same<T> = { [P in keyof T]: T[P] };

type T1<T extends Record<PropertyKey, number>> = T;
type T2<U extends Record<PropertyKey, number>> = T1<Same<U>>;

// Repro from #38235

type Foo<IdentifierT extends Record<PropertyKey, PropertyKey>> =
IdentifierT
;

type Bar<IdentifierT extends Record<PropertyKey, PropertyKey>, T> =
{
[k in keyof T] : Foo<IdentifierT & { k : k }>
}
;

type Merge2<T> = { [k in keyof T] : T[k] }
type Bar2<IdentifierT extends Record<PropertyKey, PropertyKey>, T> =
{
[k in keyof T]: Foo<Merge2<IdentifierT & { k: k }>>
}
;

type Identity<T> = T;
type Merge3<T> = Identity<{ [k in keyof T] : T[k] }>
type Bar3<IdentifierT extends Record<PropertyKey, PropertyKey>, T> =
{
[k in keyof T]: Foo<Merge3<IdentifierT & { k: k }>>
}
;