diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 3de21bebf7681..b5d42632d7fad 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -14662,7 +14662,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 0000000000000..2d367bef27c36 --- /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 0000000000000..2c96c9e53bed1 --- /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 0000000000000..207119f0cd2b2 --- /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 0000000000000..4a1a6dcdbdb84 --- /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