Instantiate generic conditional infer source types in the context of the target conditional (#31545)

* Instantiate generic conditional infer source types in the context of the target conditional

* Add test case from #26627
This commit is contained in:
Wesley Wigham 2019-08-19 13:01:08 -07:00 committed by GitHub
parent d75de60548
commit fd1e22bbf1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 253 additions and 2 deletions

View File

@ -13527,9 +13527,19 @@ namespace ts {
// Two conditional types 'T1 extends U1 ? X1 : Y1' and 'T2 extends U2 ? X2 : Y2' are related if
// one of T1 and T2 is related to the other, U1 and U2 are identical types, X1 is related to X2,
// and Y1 is related to Y2.
if (isTypeIdenticalTo((<ConditionalType>source).extendsType, (<ConditionalType>target).extendsType) &&
const sourceParams = (source as ConditionalType).root.inferTypeParameters;
let sourceExtends = (<ConditionalType>source).extendsType;
let mapper: TypeMapper | undefined;
if (sourceParams) {
// If the source has infer type parameters, we instantiate them in the context of the target
const ctx = createInferenceContext(sourceParams, /*signature*/ undefined, InferenceFlags.None, isRelatedTo);
inferTypes(ctx.inferences, (<ConditionalType>target).extendsType, sourceExtends, InferencePriority.NoConstraints | InferencePriority.AlwaysStrict);
sourceExtends = instantiateType(sourceExtends, ctx.mapper);
mapper = ctx.mapper;
}
if (isTypeIdenticalTo(sourceExtends, (<ConditionalType>target).extendsType) &&
(isRelatedTo((<ConditionalType>source).checkType, (<ConditionalType>target).checkType) || isRelatedTo((<ConditionalType>target).checkType, (<ConditionalType>source).checkType))) {
if (result = isRelatedTo(getTrueTypeFromConditionalType(<ConditionalType>source), getTrueTypeFromConditionalType(<ConditionalType>target), reportErrors)) {
if (result = isRelatedTo(instantiateType(getTrueTypeFromConditionalType(<ConditionalType>source), mapper), getTrueTypeFromConditionalType(<ConditionalType>target), reportErrors)) {
result &= isRelatedTo(getFalseTypeFromConditionalType(<ConditionalType>source), getFalseTypeFromConditionalType(<ConditionalType>target), reportErrors);
}
if (result) {

View File

@ -0,0 +1,48 @@
//// [identicalGenericConditionalsWithInferRelated.ts]
function f<X>(arg: X) {
type Cond1 = X extends [infer A] ? A : never;
type Cond2 = X extends [infer A] ? A : never;
let x: Cond1 = null as any;
let y: Cond2 = null as any;
x = y; // is err, should be ok
y = x; // is err, should be ok
}
// repro from https://github.com/microsoft/TypeScript/issues/26627
export type Constructor<T> = new (...args: any[]) => T
export type MappedResult<T> =
T extends Boolean ? boolean :
T extends Number ? number :
T extends String ? string :
T
interface X {
decode<C extends Constructor<any>>(ctor: C): MappedResult<C extends Constructor<infer T> ? T : never>
}
class Y implements X {
decode<C extends Constructor<any>>(ctor: C): MappedResult<C extends Constructor<infer T> ? T : never> {
throw new Error()
}
}
//// [identicalGenericConditionalsWithInferRelated.js]
"use strict";
exports.__esModule = true;
function f(arg) {
var x = null;
var y = null;
x = y; // is err, should be ok
y = x; // is err, should be ok
}
var Y = /** @class */ (function () {
function Y() {
}
Y.prototype.decode = function (ctor) {
throw new Error();
};
return Y;
}());

View File

@ -0,0 +1,100 @@
=== tests/cases/compiler/identicalGenericConditionalsWithInferRelated.ts ===
function f<X>(arg: X) {
>f : Symbol(f, Decl(identicalGenericConditionalsWithInferRelated.ts, 0, 0))
>X : Symbol(X, Decl(identicalGenericConditionalsWithInferRelated.ts, 0, 11))
>arg : Symbol(arg, Decl(identicalGenericConditionalsWithInferRelated.ts, 0, 14))
>X : Symbol(X, Decl(identicalGenericConditionalsWithInferRelated.ts, 0, 11))
type Cond1 = X extends [infer A] ? A : never;
>Cond1 : Symbol(Cond1, Decl(identicalGenericConditionalsWithInferRelated.ts, 0, 23))
>X : Symbol(X, Decl(identicalGenericConditionalsWithInferRelated.ts, 0, 11))
>A : Symbol(A, Decl(identicalGenericConditionalsWithInferRelated.ts, 1, 33))
>A : Symbol(A, Decl(identicalGenericConditionalsWithInferRelated.ts, 1, 33))
type Cond2 = X extends [infer A] ? A : never;
>Cond2 : Symbol(Cond2, Decl(identicalGenericConditionalsWithInferRelated.ts, 1, 49))
>X : Symbol(X, Decl(identicalGenericConditionalsWithInferRelated.ts, 0, 11))
>A : Symbol(A, Decl(identicalGenericConditionalsWithInferRelated.ts, 2, 33))
>A : Symbol(A, Decl(identicalGenericConditionalsWithInferRelated.ts, 2, 33))
let x: Cond1 = null as any;
>x : Symbol(x, Decl(identicalGenericConditionalsWithInferRelated.ts, 4, 7))
>Cond1 : Symbol(Cond1, Decl(identicalGenericConditionalsWithInferRelated.ts, 0, 23))
let y: Cond2 = null as any;
>y : Symbol(y, Decl(identicalGenericConditionalsWithInferRelated.ts, 5, 7))
>Cond2 : Symbol(Cond2, Decl(identicalGenericConditionalsWithInferRelated.ts, 1, 49))
x = y; // is err, should be ok
>x : Symbol(x, Decl(identicalGenericConditionalsWithInferRelated.ts, 4, 7))
>y : Symbol(y, Decl(identicalGenericConditionalsWithInferRelated.ts, 5, 7))
y = x; // is err, should be ok
>y : Symbol(y, Decl(identicalGenericConditionalsWithInferRelated.ts, 5, 7))
>x : Symbol(x, Decl(identicalGenericConditionalsWithInferRelated.ts, 4, 7))
}
// repro from https://github.com/microsoft/TypeScript/issues/26627
export type Constructor<T> = new (...args: any[]) => T
>Constructor : Symbol(Constructor, Decl(identicalGenericConditionalsWithInferRelated.ts, 8, 1))
>T : Symbol(T, Decl(identicalGenericConditionalsWithInferRelated.ts, 11, 24))
>args : Symbol(args, Decl(identicalGenericConditionalsWithInferRelated.ts, 11, 34))
>T : Symbol(T, Decl(identicalGenericConditionalsWithInferRelated.ts, 11, 24))
export type MappedResult<T> =
>MappedResult : Symbol(MappedResult, Decl(identicalGenericConditionalsWithInferRelated.ts, 11, 54))
>T : Symbol(T, Decl(identicalGenericConditionalsWithInferRelated.ts, 12, 25))
T extends Boolean ? boolean :
>T : Symbol(T, Decl(identicalGenericConditionalsWithInferRelated.ts, 12, 25))
>Boolean : Symbol(Boolean, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
T extends Number ? number :
>T : Symbol(T, Decl(identicalGenericConditionalsWithInferRelated.ts, 12, 25))
>Number : Symbol(Number, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
T extends String ? string :
>T : Symbol(T, Decl(identicalGenericConditionalsWithInferRelated.ts, 12, 25))
>String : Symbol(String, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
T
>T : Symbol(T, Decl(identicalGenericConditionalsWithInferRelated.ts, 12, 25))
interface X {
>X : Symbol(X, Decl(identicalGenericConditionalsWithInferRelated.ts, 16, 5))
decode<C extends Constructor<any>>(ctor: C): MappedResult<C extends Constructor<infer T> ? T : never>
>decode : Symbol(X.decode, Decl(identicalGenericConditionalsWithInferRelated.ts, 19, 13))
>C : Symbol(C, Decl(identicalGenericConditionalsWithInferRelated.ts, 20, 11))
>Constructor : Symbol(Constructor, Decl(identicalGenericConditionalsWithInferRelated.ts, 8, 1))
>ctor : Symbol(ctor, Decl(identicalGenericConditionalsWithInferRelated.ts, 20, 39))
>C : Symbol(C, Decl(identicalGenericConditionalsWithInferRelated.ts, 20, 11))
>MappedResult : Symbol(MappedResult, Decl(identicalGenericConditionalsWithInferRelated.ts, 11, 54))
>C : Symbol(C, Decl(identicalGenericConditionalsWithInferRelated.ts, 20, 11))
>Constructor : Symbol(Constructor, Decl(identicalGenericConditionalsWithInferRelated.ts, 8, 1))
>T : Symbol(T, Decl(identicalGenericConditionalsWithInferRelated.ts, 20, 89))
>T : Symbol(T, Decl(identicalGenericConditionalsWithInferRelated.ts, 20, 89))
}
class Y implements X {
>Y : Symbol(Y, Decl(identicalGenericConditionalsWithInferRelated.ts, 21, 1))
>X : Symbol(X, Decl(identicalGenericConditionalsWithInferRelated.ts, 16, 5))
decode<C extends Constructor<any>>(ctor: C): MappedResult<C extends Constructor<infer T> ? T : never> {
>decode : Symbol(Y.decode, Decl(identicalGenericConditionalsWithInferRelated.ts, 23, 22))
>C : Symbol(C, Decl(identicalGenericConditionalsWithInferRelated.ts, 24, 11))
>Constructor : Symbol(Constructor, Decl(identicalGenericConditionalsWithInferRelated.ts, 8, 1))
>ctor : Symbol(ctor, Decl(identicalGenericConditionalsWithInferRelated.ts, 24, 39))
>C : Symbol(C, Decl(identicalGenericConditionalsWithInferRelated.ts, 24, 11))
>MappedResult : Symbol(MappedResult, Decl(identicalGenericConditionalsWithInferRelated.ts, 11, 54))
>C : Symbol(C, Decl(identicalGenericConditionalsWithInferRelated.ts, 24, 11))
>Constructor : Symbol(Constructor, Decl(identicalGenericConditionalsWithInferRelated.ts, 8, 1))
>T : Symbol(T, Decl(identicalGenericConditionalsWithInferRelated.ts, 24, 89))
>T : Symbol(T, Decl(identicalGenericConditionalsWithInferRelated.ts, 24, 89))
throw new Error()
>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
}
}

View File

@ -0,0 +1,65 @@
=== tests/cases/compiler/identicalGenericConditionalsWithInferRelated.ts ===
function f<X>(arg: X) {
>f : <X>(arg: X) => void
>arg : X
type Cond1 = X extends [infer A] ? A : never;
>Cond1 : X extends [infer A] ? A : never
type Cond2 = X extends [infer A] ? A : never;
>Cond2 : X extends [infer A] ? A : never
let x: Cond1 = null as any;
>x : X extends [infer A] ? A : never
>null as any : any
>null : null
let y: Cond2 = null as any;
>y : X extends [infer A] ? A : never
>null as any : any
>null : null
x = y; // is err, should be ok
>x = y : X extends [infer A] ? A : never
>x : X extends [infer A] ? A : never
>y : X extends [infer A] ? A : never
y = x; // is err, should be ok
>y = x : X extends [infer A] ? A : never
>y : X extends [infer A] ? A : never
>x : X extends [infer A] ? A : never
}
// repro from https://github.com/microsoft/TypeScript/issues/26627
export type Constructor<T> = new (...args: any[]) => T
>Constructor : Constructor<T>
>args : any[]
export type MappedResult<T> =
>MappedResult : MappedResult<T>
T extends Boolean ? boolean :
T extends Number ? number :
T extends String ? string :
T
interface X {
decode<C extends Constructor<any>>(ctor: C): MappedResult<C extends Constructor<infer T> ? T : never>
>decode : <C extends Constructor<any>>(ctor: C) => MappedResult<C extends Constructor<infer T> ? T : never>
>ctor : C
}
class Y implements X {
>Y : Y
decode<C extends Constructor<any>>(ctor: C): MappedResult<C extends Constructor<infer T> ? T : never> {
>decode : <C extends Constructor<any>>(ctor: C) => MappedResult<C extends Constructor<infer T> ? T : never>
>ctor : C
throw new Error()
>new Error() : Error
>Error : ErrorConstructor
}
}

View File

@ -0,0 +1,28 @@
function f<X>(arg: X) {
type Cond1 = X extends [infer A] ? A : never;
type Cond2 = X extends [infer A] ? A : never;
let x: Cond1 = null as any;
let y: Cond2 = null as any;
x = y; // is err, should be ok
y = x; // is err, should be ok
}
// repro from https://github.com/microsoft/TypeScript/issues/26627
export type Constructor<T> = new (...args: any[]) => T
export type MappedResult<T> =
T extends Boolean ? boolean :
T extends Number ? number :
T extends String ? string :
T
interface X {
decode<C extends Constructor<any>>(ctor: C): MappedResult<C extends Constructor<infer T> ? T : never>
}
class Y implements X {
decode<C extends Constructor<any>>(ctor: C): MappedResult<C extends Constructor<infer T> ? T : never> {
throw new Error()
}
}