Merge pull request #32260 from microsoft/fix32169

Include conditional types in top-level type parameter check
This commit is contained in:
Anders Hejlsberg 2019-07-09 10:30:44 -07:00 committed by GitHub
commit b0f050f4ee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 78 additions and 1 deletions

View File

@ -15286,7 +15286,11 @@ namespace ts {
}
function isTypeParameterAtTopLevel(type: Type, typeParameter: TypeParameter): boolean {
return type === typeParameter || !!(type.flags & TypeFlags.UnionOrIntersection) && some((<UnionOrIntersectionType>type).types, t => isTypeParameterAtTopLevel(t, typeParameter));
return !!(type === typeParameter ||
type.flags & TypeFlags.UnionOrIntersection && some((<UnionOrIntersectionType>type).types, t => isTypeParameterAtTopLevel(t, typeParameter)) ||
type.flags & TypeFlags.Conditional && (
isTypeParameterAtTopLevel(getTrueTypeFromConditionalType(<ConditionalType>type), typeParameter) ||
isTypeParameterAtTopLevel(getFalseTypeFromConditionalType(<ConditionalType>type), typeParameter)));
}
/** Create an object with properties named in the string literal type. Every property has type `any` */

View File

@ -135,6 +135,13 @@ function test<T extends { a: string, b: string }>(obj: T): T {
let { a, ...rest } = obj;
return { a: 'hello', ...rest } as T;
}
// Repro from #32169
declare function f<T>(x: T): NonNullable<T>;
enum E { A, B }
const a = f(E.A);
const b: E.A = a;
//// [literalTypeWidening.js]
@ -267,3 +274,10 @@ function test(obj) {
var a = obj.a, rest = __rest(obj, ["a"]);
return __assign({ a: 'hello' }, rest);
}
var E;
(function (E) {
E[E["A"] = 0] = "A";
E[E["B"] = 1] = "B";
})(E || (E = {}));
var a = f(E.A);
var b = a;

View File

@ -422,3 +422,31 @@ function test<T extends { a: string, b: string }>(obj: T): T {
>T : Symbol(T, Decl(literalTypeWidening.ts, 132, 14))
}
// Repro from #32169
declare function f<T>(x: T): NonNullable<T>;
>f : Symbol(f, Decl(literalTypeWidening.ts, 135, 1))
>T : Symbol(T, Decl(literalTypeWidening.ts, 139, 19))
>x : Symbol(x, Decl(literalTypeWidening.ts, 139, 22))
>T : Symbol(T, Decl(literalTypeWidening.ts, 139, 19))
>NonNullable : Symbol(NonNullable, Decl(lib.es5.d.ts, --, --))
>T : Symbol(T, Decl(literalTypeWidening.ts, 139, 19))
enum E { A, B }
>E : Symbol(E, Decl(literalTypeWidening.ts, 139, 44))
>A : Symbol(E.A, Decl(literalTypeWidening.ts, 140, 8))
>B : Symbol(E.B, Decl(literalTypeWidening.ts, 140, 11))
const a = f(E.A);
>a : Symbol(a, Decl(literalTypeWidening.ts, 141, 5))
>f : Symbol(f, Decl(literalTypeWidening.ts, 135, 1))
>E.A : Symbol(E.A, Decl(literalTypeWidening.ts, 140, 8))
>E : Symbol(E, Decl(literalTypeWidening.ts, 139, 44))
>A : Symbol(E.A, Decl(literalTypeWidening.ts, 140, 8))
const b: E.A = a;
>b : Symbol(b, Decl(literalTypeWidening.ts, 142, 5))
>E : Symbol(E, Decl(literalTypeWidening.ts, 139, 44))
>A : Symbol(E.A, Decl(literalTypeWidening.ts, 140, 8))
>a : Symbol(a, Decl(literalTypeWidening.ts, 141, 5))

View File

@ -454,3 +454,27 @@ function test<T extends { a: string, b: string }>(obj: T): T {
>rest : Pick<T, Exclude<keyof T, "a">>
}
// Repro from #32169
declare function f<T>(x: T): NonNullable<T>;
>f : <T>(x: T) => NonNullable<T>
>x : T
enum E { A, B }
>E : E
>A : E.A
>B : E.B
const a = f(E.A);
>a : E.A
>f(E.A) : E.A
>f : <T>(x: T) => NonNullable<T>
>E.A : E.A
>E : typeof E
>A : E.A
const b: E.A = a;
>b : E.A
>E : any
>a : E.A

View File

@ -134,3 +134,10 @@ function test<T extends { a: string, b: string }>(obj: T): T {
let { a, ...rest } = obj;
return { a: 'hello', ...rest } as T;
}
// Repro from #32169
declare function f<T>(x: T): NonNullable<T>;
enum E { A, B }
const a = f(E.A);
const b: E.A = a;