From b9f805f2f44a4ae94f8215d63df52abd827a9c2f Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Sat, 13 Apr 2019 08:15:28 -1000 Subject: [PATCH 1/3] Remove incorrect optimization in getUnionConstraintOfIntersection --- src/compiler/checker.ts | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 7becc4a2f78..72e85b3f8a8 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -7598,11 +7598,6 @@ namespace ts { constraint = getConstraintOfType(constraint); } if (constraint) { - // A constraint that isn't a union type implies that the final type would be a non-union - // type as well. Since non-union constraints are of no interest, we can exit here. - if (!(constraint.flags & TypeFlags.Union)) { - return undefined; - } constraints = append(constraints, constraint); } } From 2c951b3ca99751c98a23d1bb707803264f0f6bdf Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Sat, 13 Apr 2019 08:24:27 -1000 Subject: [PATCH 2/3] Add regression test --- .../types/intersection/intersectionWithUnionConstraint.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/cases/conformance/types/intersection/intersectionWithUnionConstraint.ts b/tests/cases/conformance/types/intersection/intersectionWithUnionConstraint.ts index 278d72595cd..2a756788f6f 100644 --- a/tests/cases/conformance/types/intersection/intersectionWithUnionConstraint.ts +++ b/tests/cases/conformance/types/intersection/intersectionWithUnionConstraint.ts @@ -34,3 +34,8 @@ type Example = { [K in keyof T]: K extends keyof U ? UnexpectedError : type UnexpectedError = T type NoErrorHere = T + +// Repro from #30331 + +type a = T extends Array ? U : never; +type b = { [K in a & keyof T ]: 42 }; From f3fd27c0eef6f1d8ad953df85fe339b9c4aaaabb Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Sat, 13 Apr 2019 08:24:34 -1000 Subject: [PATCH 3/3] Accept new baselines --- .../intersectionWithUnionConstraint.errors.txt | 5 +++++ .../intersectionWithUnionConstraint.js | 5 +++++ .../intersectionWithUnionConstraint.symbols | 18 ++++++++++++++++++ .../intersectionWithUnionConstraint.types | 8 ++++++++ 4 files changed, 36 insertions(+) diff --git a/tests/baselines/reference/intersectionWithUnionConstraint.errors.txt b/tests/baselines/reference/intersectionWithUnionConstraint.errors.txt index 9bdfc6f30f9..97ed1cf4895 100644 --- a/tests/baselines/reference/intersectionWithUnionConstraint.errors.txt +++ b/tests/baselines/reference/intersectionWithUnionConstraint.errors.txt @@ -80,4 +80,9 @@ tests/cases/conformance/types/intersection/intersectionWithUnionConstraint.ts(12 type UnexpectedError = T type NoErrorHere = T + + // Repro from #30331 + + type a = T extends Array ? U : never; + type b = { [K in a & keyof T ]: 42 }; \ No newline at end of file diff --git a/tests/baselines/reference/intersectionWithUnionConstraint.js b/tests/baselines/reference/intersectionWithUnionConstraint.js index fc3e50309c6..00330d23f6d 100644 --- a/tests/baselines/reference/intersectionWithUnionConstraint.js +++ b/tests/baselines/reference/intersectionWithUnionConstraint.js @@ -33,6 +33,11 @@ type Example = { [K in keyof T]: K extends keyof U ? UnexpectedError : type UnexpectedError = T type NoErrorHere = T + +// Repro from #30331 + +type a = T extends Array ? U : never; +type b = { [K in a & keyof T ]: 42 }; //// [intersectionWithUnionConstraint.js] diff --git a/tests/baselines/reference/intersectionWithUnionConstraint.symbols b/tests/baselines/reference/intersectionWithUnionConstraint.symbols index f481a5c5185..1ef1f8435d9 100644 --- a/tests/baselines/reference/intersectionWithUnionConstraint.symbols +++ b/tests/baselines/reference/intersectionWithUnionConstraint.symbols @@ -112,3 +112,21 @@ type NoErrorHere = T >PropertyKey : Symbol(PropertyKey, Decl(lib.es5.d.ts, --, --)) >T : Symbol(T, Decl(intersectionWithUnionConstraint.ts, 33, 17)) +// Repro from #30331 + +type a = T extends Array ? U : never; +>a : Symbol(a, Decl(intersectionWithUnionConstraint.ts, 33, 43)) +>T : Symbol(T, Decl(intersectionWithUnionConstraint.ts, 37, 7)) +>T : Symbol(T, Decl(intersectionWithUnionConstraint.ts, 37, 7)) +>Array : Symbol(Array, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) +>U : Symbol(U, Decl(intersectionWithUnionConstraint.ts, 37, 33)) +>U : Symbol(U, Decl(intersectionWithUnionConstraint.ts, 37, 33)) + +type b = { [K in a & keyof T ]: 42 }; +>b : Symbol(b, Decl(intersectionWithUnionConstraint.ts, 37, 49)) +>T : Symbol(T, Decl(intersectionWithUnionConstraint.ts, 38, 7)) +>K : Symbol(K, Decl(intersectionWithUnionConstraint.ts, 38, 15)) +>a : Symbol(a, Decl(intersectionWithUnionConstraint.ts, 33, 43)) +>T : Symbol(T, Decl(intersectionWithUnionConstraint.ts, 38, 7)) +>T : Symbol(T, Decl(intersectionWithUnionConstraint.ts, 38, 7)) + diff --git a/tests/baselines/reference/intersectionWithUnionConstraint.types b/tests/baselines/reference/intersectionWithUnionConstraint.types index 126154a4968..0f9e9974bed 100644 --- a/tests/baselines/reference/intersectionWithUnionConstraint.types +++ b/tests/baselines/reference/intersectionWithUnionConstraint.types @@ -84,3 +84,11 @@ type UnexpectedError = T type NoErrorHere = T >NoErrorHere : T +// Repro from #30331 + +type a = T extends Array ? U : never; +>a : a + +type b = { [K in a & keyof T ]: 42 }; +>b : b +