From 408c804103d5b4f3ce97722190eab9d428740d80 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Fri, 5 Mar 2021 07:26:27 -0800 Subject: [PATCH] Properly instantiate inferred constraints in conditional types (#42747) * fix combined type mapper in getConditionalType * Add regression tests --- src/compiler/checker.ts | 5 +- .../inferTypeParameterConstraints.js | 23 +++++++++ .../inferTypeParameterConstraints.symbols | 51 +++++++++++++++++++ .../inferTypeParameterConstraints.types | 26 ++++++++++ .../compiler/inferTypeParameterConstraints.ts | 19 +++++++ 5 files changed, 123 insertions(+), 1 deletion(-) create mode 100644 tests/baselines/reference/inferTypeParameterConstraints.js create mode 100644 tests/baselines/reference/inferTypeParameterConstraints.symbols create mode 100644 tests/baselines/reference/inferTypeParameterConstraints.types create mode 100644 tests/cases/compiler/inferTypeParameterConstraints.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 3633ce34794..fd2c5aee3e8 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -14796,7 +14796,10 @@ namespace ts { // types rules (i.e. proper contravariance) for inferences. inferTypes(context.inferences, checkType, extendsType, InferencePriority.NoConstraints | InferencePriority.AlwaysStrict); } - combinedMapper = mergeTypeMappers(mapper, context.mapper); + // It's possible for 'infer T' type paramteters to be given uninstantiated constraints when the + // those type parameters are used in type references (see getInferredTypeParameterConstraint). For + // that reason we need context.mapper to be first in the combined mapper. See #42636 for examples. + combinedMapper = mapper ? combineTypeMappers(context.mapper, mapper) : context.mapper; } // Instantiate the extends type including inferences for 'infer T' type parameters const inferredExtendsType = combinedMapper ? instantiateType(unwrapNondistributiveConditionalTuple(root, root.extendsType), combinedMapper) : extendsType; diff --git a/tests/baselines/reference/inferTypeParameterConstraints.js b/tests/baselines/reference/inferTypeParameterConstraints.js new file mode 100644 index 00000000000..2d367bef27c --- /dev/null +++ b/tests/baselines/reference/inferTypeParameterConstraints.js @@ -0,0 +1,23 @@ +//// [inferTypeParameterConstraints.ts] +// Repro from #42636 + +type SubGuard = X; + +type IsSub = M extends [...SubGuard, ...S, ...any[]] ? B : never; + +type E0 = IsSub<[1, 2, 3, 4], [2, 3, 4]>; // [1 | 2 | 3 | 4] + +type E1 = [1, 2, 3, 4] extends [...infer B, 2, 3, 4, ...any[]] ? B : never; // unknown[] + +// Repro from #42636 + +type Constrain = unknown; + +type Foo = A extends Constrain ? X : never; + +type T0 = Foo; // string + + +//// [inferTypeParameterConstraints.js] +"use strict"; +// Repro from #42636 diff --git a/tests/baselines/reference/inferTypeParameterConstraints.symbols b/tests/baselines/reference/inferTypeParameterConstraints.symbols new file mode 100644 index 00000000000..2c96c9e53be --- /dev/null +++ b/tests/baselines/reference/inferTypeParameterConstraints.symbols @@ -0,0 +1,51 @@ +=== tests/cases/compiler/inferTypeParameterConstraints.ts === +// Repro from #42636 + +type SubGuard = X; +>SubGuard : Symbol(SubGuard, Decl(inferTypeParameterConstraints.ts, 0, 0)) +>A : Symbol(A, Decl(inferTypeParameterConstraints.ts, 2, 14)) +>X : Symbol(X, Decl(inferTypeParameterConstraints.ts, 2, 16)) +>A : Symbol(A, Decl(inferTypeParameterConstraints.ts, 2, 14)) +>X : Symbol(X, Decl(inferTypeParameterConstraints.ts, 2, 16)) + +type IsSub = M extends [...SubGuard, ...S, ...any[]] ? B : never; +>IsSub : Symbol(IsSub, Decl(inferTypeParameterConstraints.ts, 2, 36)) +>M : Symbol(M, Decl(inferTypeParameterConstraints.ts, 4, 11)) +>S : Symbol(S, Decl(inferTypeParameterConstraints.ts, 4, 27)) +>M : Symbol(M, Decl(inferTypeParameterConstraints.ts, 4, 11)) +>SubGuard : Symbol(SubGuard, Decl(inferTypeParameterConstraints.ts, 0, 0)) +>M : Symbol(M, Decl(inferTypeParameterConstraints.ts, 4, 11)) +>B : Symbol(B, Decl(inferTypeParameterConstraints.ts, 4, 86)) +>S : Symbol(S, Decl(inferTypeParameterConstraints.ts, 4, 27)) +>B : Symbol(B, Decl(inferTypeParameterConstraints.ts, 4, 86)) + +type E0 = IsSub<[1, 2, 3, 4], [2, 3, 4]>; // [1 | 2 | 3 | 4] +>E0 : Symbol(E0, Decl(inferTypeParameterConstraints.ts, 4, 119)) +>IsSub : Symbol(IsSub, Decl(inferTypeParameterConstraints.ts, 2, 36)) + +type E1 = [1, 2, 3, 4] extends [...infer B, 2, 3, 4, ...any[]] ? B : never; // unknown[] +>E1 : Symbol(E1, Decl(inferTypeParameterConstraints.ts, 6, 41)) +>B : Symbol(B, Decl(inferTypeParameterConstraints.ts, 8, 40)) +>B : Symbol(B, Decl(inferTypeParameterConstraints.ts, 8, 40)) + +// Repro from #42636 + +type Constrain = unknown; +>Constrain : Symbol(Constrain, Decl(inferTypeParameterConstraints.ts, 8, 75)) +>T : Symbol(T, Decl(inferTypeParameterConstraints.ts, 12, 15)) +>C : Symbol(C, Decl(inferTypeParameterConstraints.ts, 12, 27)) +>C : Symbol(C, Decl(inferTypeParameterConstraints.ts, 12, 27)) + +type Foo = A extends Constrain ? X : never; +>Foo : Symbol(Foo, Decl(inferTypeParameterConstraints.ts, 12, 41)) +>A : Symbol(A, Decl(inferTypeParameterConstraints.ts, 14, 9)) +>A : Symbol(A, Decl(inferTypeParameterConstraints.ts, 14, 9)) +>Constrain : Symbol(Constrain, Decl(inferTypeParameterConstraints.ts, 8, 75)) +>X : Symbol(X, Decl(inferTypeParameterConstraints.ts, 14, 39)) +>A : Symbol(A, Decl(inferTypeParameterConstraints.ts, 14, 9)) +>X : Symbol(X, Decl(inferTypeParameterConstraints.ts, 14, 39)) + +type T0 = Foo; // string +>T0 : Symbol(T0, Decl(inferTypeParameterConstraints.ts, 14, 58)) +>Foo : Symbol(Foo, Decl(inferTypeParameterConstraints.ts, 12, 41)) + diff --git a/tests/baselines/reference/inferTypeParameterConstraints.types b/tests/baselines/reference/inferTypeParameterConstraints.types new file mode 100644 index 00000000000..207119f0cd2 --- /dev/null +++ b/tests/baselines/reference/inferTypeParameterConstraints.types @@ -0,0 +1,26 @@ +=== tests/cases/compiler/inferTypeParameterConstraints.ts === +// Repro from #42636 + +type SubGuard = X; +>SubGuard : X + +type IsSub = M extends [...SubGuard, ...S, ...any[]] ? B : never; +>IsSub : IsSub + +type E0 = IsSub<[1, 2, 3, 4], [2, 3, 4]>; // [1 | 2 | 3 | 4] +>E0 : [1 | 4 | 2 | 3] + +type E1 = [1, 2, 3, 4] extends [...infer B, 2, 3, 4, ...any[]] ? B : never; // unknown[] +>E1 : unknown[] + +// Repro from #42636 + +type Constrain = unknown; +>Constrain : unknown + +type Foo = A extends Constrain ? X : never; +>Foo : Foo + +type T0 = Foo; // string +>T0 : string + diff --git a/tests/cases/compiler/inferTypeParameterConstraints.ts b/tests/cases/compiler/inferTypeParameterConstraints.ts new file mode 100644 index 00000000000..4a1a6dcdbdb --- /dev/null +++ b/tests/cases/compiler/inferTypeParameterConstraints.ts @@ -0,0 +1,19 @@ +// @strict: true + +// Repro from #42636 + +type SubGuard = X; + +type IsSub = M extends [...SubGuard, ...S, ...any[]] ? B : never; + +type E0 = IsSub<[1, 2, 3, 4], [2, 3, 4]>; // [1 | 2 | 3 | 4] + +type E1 = [1, 2, 3, 4] extends [...infer B, 2, 3, 4, ...any[]] ? B : never; // unknown[] + +// Repro from #42636 + +type Constrain = unknown; + +type Foo = A extends Constrain ? X : never; + +type T0 = Foo; // string