Skip to content

Properly instantiate inferred constraints in conditional types #42747

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
23 changes: 23 additions & 0 deletions tests/baselines/reference/inferTypeParameterConstraints.js
Original file line number Diff line number Diff line change
@@ -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
51 changes: 51 additions & 0 deletions tests/baselines/reference/inferTypeParameterConstraints.symbols
Original file line number Diff line number Diff line change
@@ -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))

26 changes: 26 additions & 0 deletions tests/baselines/reference/inferTypeParameterConstraints.types
Original file line number Diff line number Diff line change
@@ -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

19 changes: 19 additions & 0 deletions tests/cases/compiler/inferTypeParameterConstraints.ts
Original file line number Diff line number Diff line change
@@ -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