From 8120094c81236651da967c33d7bd5ca185188faf Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Wed, 22 May 2019 06:49:49 -0700 Subject: [PATCH 1/4] Simplify index and object types when obtaining indexed access constraint --- src/compiler/checker.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index e9dddff64af3..bd8d4c05901c 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -7657,15 +7657,20 @@ namespace ts { return hasNonCircularBaseConstraint(type) ? getConstraintFromIndexedAccess(type) : undefined; } + function getSimplifiedTypeOrConstraint(type: Type) { + const simplified = getSimplifiedType(type, /*writing*/ false); + return simplified !== type ? simplified : getConstraintOfType(type); + } + function getConstraintFromIndexedAccess(type: IndexedAccessType) { - const indexConstraint = getConstraintOfType(type.indexType); + const indexConstraint = getSimplifiedTypeOrConstraint(type.indexType); if (indexConstraint && indexConstraint !== type.indexType) { const indexedAccess = getIndexedAccessTypeOrUndefined(type.objectType, indexConstraint); if (indexedAccess) { return indexedAccess; } } - const objectConstraint = getConstraintOfType(type.objectType); + const objectConstraint = getSimplifiedTypeOrConstraint(type.objectType); if (objectConstraint && objectConstraint !== type.objectType) { return getIndexedAccessTypeOrUndefined(objectConstraint, type.indexType); } From 2fd4aaee92a17318ad1c45c6f6590bbb8b82d33d Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Wed, 22 May 2019 06:54:16 -0700 Subject: [PATCH 2/4] Add regression test --- .../conformance/types/keyof/keyofAndIndexedAccess2.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts b/tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts index a99c2b0a7a10..898648cea44f 100644 --- a/tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts +++ b/tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts @@ -147,3 +147,13 @@ export class c { this["a"] = "b"; } } + +// Repro from #31385 + +type Foo = { [key: string]: { [K in keyof T]: K }[keyof T] }; + +type Bar = { [key: string]: { [K in keyof T]: [K] }[keyof T] }; + +type Baz> = { [K in keyof Q]: T[Q[K]] }; + +type Qux> = { [K in keyof Q]: T[Q[K]["0"]] }; From b7012b577a6e755215f80d3166e90f60aa50b98e Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Wed, 22 May 2019 06:54:23 -0700 Subject: [PATCH 3/4] Accept new baselines --- .../keyofAndIndexedAccess2.errors.txt | 10 +++++ .../reference/keyofAndIndexedAccess2.js | 10 +++++ .../reference/keyofAndIndexedAccess2.symbols | 44 +++++++++++++++++++ .../reference/keyofAndIndexedAccess2.types | 16 +++++++ 4 files changed, 80 insertions(+) diff --git a/tests/baselines/reference/keyofAndIndexedAccess2.errors.txt b/tests/baselines/reference/keyofAndIndexedAccess2.errors.txt index 9ebf28f1857e..1cad297c52d2 100644 --- a/tests/baselines/reference/keyofAndIndexedAccess2.errors.txt +++ b/tests/baselines/reference/keyofAndIndexedAccess2.errors.txt @@ -228,4 +228,14 @@ tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(108,5): error TS23 this["a"] = "b"; } } + + // Repro from #31385 + + type Foo = { [key: string]: { [K in keyof T]: K }[keyof T] }; + + type Bar = { [key: string]: { [K in keyof T]: [K] }[keyof T] }; + + type Baz> = { [K in keyof Q]: T[Q[K]] }; + + type Qux> = { [K in keyof Q]: T[Q[K]["0"]] }; \ No newline at end of file diff --git a/tests/baselines/reference/keyofAndIndexedAccess2.js b/tests/baselines/reference/keyofAndIndexedAccess2.js index 374b13b5a643..7547a3f84c59 100644 --- a/tests/baselines/reference/keyofAndIndexedAccess2.js +++ b/tests/baselines/reference/keyofAndIndexedAccess2.js @@ -145,6 +145,16 @@ export class c { this["a"] = "b"; } } + +// Repro from #31385 + +type Foo = { [key: string]: { [K in keyof T]: K }[keyof T] }; + +type Bar = { [key: string]: { [K in keyof T]: [K] }[keyof T] }; + +type Baz> = { [K in keyof Q]: T[Q[K]] }; + +type Qux> = { [K in keyof Q]: T[Q[K]["0"]] }; //// [keyofAndIndexedAccess2.js] diff --git a/tests/baselines/reference/keyofAndIndexedAccess2.symbols b/tests/baselines/reference/keyofAndIndexedAccess2.symbols index a3412e8a47d3..91602f93e1d8 100644 --- a/tests/baselines/reference/keyofAndIndexedAccess2.symbols +++ b/tests/baselines/reference/keyofAndIndexedAccess2.symbols @@ -517,3 +517,47 @@ export class c { } } +// Repro from #31385 + +type Foo = { [key: string]: { [K in keyof T]: K }[keyof T] }; +>Foo : Symbol(Foo, Decl(keyofAndIndexedAccess2.ts, 145, 1)) +>T : Symbol(T, Decl(keyofAndIndexedAccess2.ts, 149, 9)) +>key : Symbol(key, Decl(keyofAndIndexedAccess2.ts, 149, 17)) +>K : Symbol(K, Decl(keyofAndIndexedAccess2.ts, 149, 34)) +>T : Symbol(T, Decl(keyofAndIndexedAccess2.ts, 149, 9)) +>K : Symbol(K, Decl(keyofAndIndexedAccess2.ts, 149, 34)) +>T : Symbol(T, Decl(keyofAndIndexedAccess2.ts, 149, 9)) + +type Bar = { [key: string]: { [K in keyof T]: [K] }[keyof T] }; +>Bar : Symbol(Bar, Decl(keyofAndIndexedAccess2.ts, 149, 64)) +>T : Symbol(T, Decl(keyofAndIndexedAccess2.ts, 151, 9)) +>key : Symbol(key, Decl(keyofAndIndexedAccess2.ts, 151, 17)) +>K : Symbol(K, Decl(keyofAndIndexedAccess2.ts, 151, 34)) +>T : Symbol(T, Decl(keyofAndIndexedAccess2.ts, 151, 9)) +>K : Symbol(K, Decl(keyofAndIndexedAccess2.ts, 151, 34)) +>T : Symbol(T, Decl(keyofAndIndexedAccess2.ts, 151, 9)) + +type Baz> = { [K in keyof Q]: T[Q[K]] }; +>Baz : Symbol(Baz, Decl(keyofAndIndexedAccess2.ts, 151, 66)) +>T : Symbol(T, Decl(keyofAndIndexedAccess2.ts, 153, 9)) +>Q : Symbol(Q, Decl(keyofAndIndexedAccess2.ts, 153, 11)) +>Foo : Symbol(Foo, Decl(keyofAndIndexedAccess2.ts, 145, 1)) +>T : Symbol(T, Decl(keyofAndIndexedAccess2.ts, 153, 9)) +>K : Symbol(K, Decl(keyofAndIndexedAccess2.ts, 153, 35)) +>Q : Symbol(Q, Decl(keyofAndIndexedAccess2.ts, 153, 11)) +>T : Symbol(T, Decl(keyofAndIndexedAccess2.ts, 153, 9)) +>Q : Symbol(Q, Decl(keyofAndIndexedAccess2.ts, 153, 11)) +>K : Symbol(K, Decl(keyofAndIndexedAccess2.ts, 153, 35)) + +type Qux> = { [K in keyof Q]: T[Q[K]["0"]] }; +>Qux : Symbol(Qux, Decl(keyofAndIndexedAccess2.ts, 153, 60)) +>T : Symbol(T, Decl(keyofAndIndexedAccess2.ts, 155, 9)) +>Q : Symbol(Q, Decl(keyofAndIndexedAccess2.ts, 155, 11)) +>Bar : Symbol(Bar, Decl(keyofAndIndexedAccess2.ts, 149, 64)) +>T : Symbol(T, Decl(keyofAndIndexedAccess2.ts, 155, 9)) +>K : Symbol(K, Decl(keyofAndIndexedAccess2.ts, 155, 35)) +>Q : Symbol(Q, Decl(keyofAndIndexedAccess2.ts, 155, 11)) +>T : Symbol(T, Decl(keyofAndIndexedAccess2.ts, 155, 9)) +>Q : Symbol(Q, Decl(keyofAndIndexedAccess2.ts, 155, 11)) +>K : Symbol(K, Decl(keyofAndIndexedAccess2.ts, 155, 35)) + diff --git a/tests/baselines/reference/keyofAndIndexedAccess2.types b/tests/baselines/reference/keyofAndIndexedAccess2.types index b7b71ba4f9f7..45cf1d4bad5d 100644 --- a/tests/baselines/reference/keyofAndIndexedAccess2.types +++ b/tests/baselines/reference/keyofAndIndexedAccess2.types @@ -517,3 +517,19 @@ export class c { } } +// Repro from #31385 + +type Foo = { [key: string]: { [K in keyof T]: K }[keyof T] }; +>Foo : Foo +>key : string + +type Bar = { [key: string]: { [K in keyof T]: [K] }[keyof T] }; +>Bar : Bar +>key : string + +type Baz> = { [K in keyof Q]: T[Q[K]] }; +>Baz : Baz + +type Qux> = { [K in keyof Q]: T[Q[K]["0"]] }; +>Qux : Qux + From cd7a14ac21974209fc870642e6d1489a66f91af1 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Wed, 22 May 2019 17:22:33 -0700 Subject: [PATCH 4/4] Reuse getSimplifiedTypeOrConstraint function --- src/compiler/checker.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index bd8d4c05901c..386beb7f24dd 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -13066,8 +13066,7 @@ namespace ts { } // A type S is assignable to keyof T if S is assignable to keyof C, where C is the // simplified form of T or, if T doesn't simplify, the constraint of T. - const simplified = getSimplifiedType((target).type, /*writing*/ false); - const constraint = simplified !== (target).type ? simplified : getConstraintOfType((target).type); + const constraint = getSimplifiedTypeOrConstraint((target).type); if (constraint) { // We require Ternary.True here such that circular constraints don't cause // false positives. For example, given 'T extends { [K in keyof T]: string }',