Properly instantiate inferred constraints in conditional types (#42747)

* fix combined type mapper in getConditionalType

* Add regression tests
This commit is contained in:
Anders Hejlsberg
2021-03-05 07:26:27 -08:00
committed by GitHub
parent f0a72e2450
commit 408c804103
5 changed files with 123 additions and 1 deletions
+4 -1
View File
@@ -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;
@@ -0,0 +1,23 @@
//// [inferTypeParameterConstraints.ts]
// Repro from #42636
type SubGuard<A, X extends [A]> = X;
type IsSub<M extends any[], S extends any[]> = M extends [...SubGuard<M[number], infer B>, ...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<T extends C, C> = unknown;
type Foo<A> = A extends Constrain<infer X, A> ? X : never;
type T0 = Foo<string>; // string
//// [inferTypeParameterConstraints.js]
"use strict";
// Repro from #42636
@@ -0,0 +1,51 @@
=== tests/cases/compiler/inferTypeParameterConstraints.ts ===
// Repro from #42636
type SubGuard<A, X extends [A]> = 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 any[], S extends any[]> = M extends [...SubGuard<M[number], infer B>, ...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<T extends C, C> = 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> = A extends Constrain<infer X, A> ? 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>; // string
>T0 : Symbol(T0, Decl(inferTypeParameterConstraints.ts, 14, 58))
>Foo : Symbol(Foo, Decl(inferTypeParameterConstraints.ts, 12, 41))
@@ -0,0 +1,26 @@
=== tests/cases/compiler/inferTypeParameterConstraints.ts ===
// Repro from #42636
type SubGuard<A, X extends [A]> = X;
>SubGuard : X
type IsSub<M extends any[], S extends any[]> = M extends [...SubGuard<M[number], infer B>, ...S, ...any[]] ? B : never;
>IsSub : IsSub<M, S>
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<T extends C, C> = unknown;
>Constrain : unknown
type Foo<A> = A extends Constrain<infer X, A> ? X : never;
>Foo : Foo<A>
type T0 = Foo<string>; // string
>T0 : string
@@ -0,0 +1,19 @@
// @strict: true
// Repro from #42636
type SubGuard<A, X extends [A]> = X;
type IsSub<M extends any[], S extends any[]> = M extends [...SubGuard<M[number], infer B>, ...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<T extends C, C> = unknown;
type Foo<A> = A extends Constrain<infer X, A> ? X : never;
type T0 = Foo<string>; // string