Skip to content

Include conditional types in top-level type parameter check #32260

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 3 commits into from
Jul 9, 2019
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
6 changes: 5 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15208,7 +15208,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` */
Expand Down
14 changes: 14 additions & 0 deletions tests/baselines/reference/literalTypeWidening.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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;
28 changes: 28 additions & 0 deletions tests/baselines/reference/literalTypeWidening.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -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))

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

7 changes: 7 additions & 0 deletions tests/cases/conformance/types/literal/literalTypeWidening.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;