Skip to content

Display 'nothing' for the empty union type #8340

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
Apr 27, 2016
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
11 changes: 9 additions & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2014,7 +2014,12 @@ namespace ts {
writeUnionOrIntersectionType(<UnionOrIntersectionType>type, flags);
}
else if (type.flags & TypeFlags.Anonymous) {
writeAnonymousType(<ObjectType>type, flags);
if (type === emptyUnionType) {
writer.writeKeyword("nothing");
}
else {
writeAnonymousType(<ObjectType>type, flags);
}
}
else if (type.flags & TypeFlags.StringLiteral) {
writer.writeStringLiteral(`"${escapeString((<StringLiteralType>type).text)}"`);
Expand Down Expand Up @@ -4247,7 +4252,9 @@ namespace ts {
propTypes.push(getTypeOfSymbol(prop));
}
}
return getUnionType(propTypes);
if (propTypes.length) {
return getUnionType(propTypes);
}
}
return undefined;
}
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/instanceOfAssignability.types
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ function fn5(x: Derived1) {
// 1.5: y: Derived1
// Want: ???
let y = x;
>y : {}
>x : {}
>y : nothing
>x : nothing
}
}

Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/stringLiteralTypesAsTags01.types
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,6 @@ if (!hasKind(x, "B")) {
}
else {
let d = x;
>d : {}
>x : {}
>d : nothing
>x : nothing
}
4 changes: 2 additions & 2 deletions tests/baselines/reference/stringLiteralTypesAsTags02.types
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,6 @@ if (!hasKind(x, "B")) {
}
else {
let d = x;
>d : {}
>x : {}
>d : nothing
>x : nothing
}
4 changes: 2 additions & 2 deletions tests/baselines/reference/stringLiteralTypesAsTags03.types
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,6 @@ if (!hasKind(x, "B")) {
}
else {
let d = x;
>d : {}
>x : {}
>d : nothing
>x : nothing
}
4 changes: 2 additions & 2 deletions tests/baselines/reference/typeGuardOfFormTypeOfBoolean.types
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ if (typeof strOrNum === "boolean") {

let z1: {} = strOrNum; // {}
>z1 : {}
>strOrNum : {}
>strOrNum : nothing
}
else {
let z2: string | number = strOrNum; // string | number
Expand Down Expand Up @@ -215,6 +215,6 @@ if (typeof strOrNum !== "boolean") {
else {
let z2: {} = strOrNum; // {}
>z2 : {}
>strOrNum : {}
>strOrNum : nothing
}

4 changes: 2 additions & 2 deletions tests/baselines/reference/typeGuardOfFormTypeOfNumber.types
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ if (typeof strOrBool === "number") {

let y1: {} = strOrBool; // {}
>y1 : {}
>strOrBool : {}
>strOrBool : nothing
}
else {
let y2: string | boolean = strOrBool; // string | boolean
Expand Down Expand Up @@ -212,6 +212,6 @@ if (typeof strOrBool !== "number") {
else {
let y2: {} = strOrBool; // {}
>y2 : {}
>strOrBool : {}
>strOrBool : nothing
}

4 changes: 2 additions & 2 deletions tests/baselines/reference/typeGuardOfFormTypeOfOther.types
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ if (typeof strOrNumOrBool === "Object") {

let q1: {} = strOrNumOrBool; // {}
>q1 : {}
>strOrNumOrBool : {}
>strOrNumOrBool : nothing
}
else {
let q2: string | number | boolean = strOrNumOrBool; // string | number | boolean
Expand Down Expand Up @@ -178,6 +178,6 @@ if (typeof strOrNumOrBool !== "Object") {
else {
let q2: {} = strOrNumOrBool; // {}
>q2 : {}
>strOrNumOrBool : {}
>strOrNumOrBool : nothing
}

4 changes: 2 additions & 2 deletions tests/baselines/reference/typeGuardOfFormTypeOfString.types
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ if (typeof numOrBool === "string") {

let x1: {} = numOrBool; // {}
>x1 : {}
>numOrBool : {}
>numOrBool : nothing
}
else {
let x2: number | boolean = numOrBool; // number | boolean
Expand Down Expand Up @@ -214,6 +214,6 @@ if (typeof numOrBool !== "string") {
else {
let x2: {} = numOrBool; // {}
>x2 : {}
>numOrBool : {}
>numOrBool : nothing
}

Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ if (typeof stringOrNumber === "number") {
>"number" : string

stringOrNumber;
>stringOrNumber : {}
>stringOrNumber : nothing
}
}

Expand All @@ -31,6 +31,6 @@ if (typeof stringOrNumber === "number" && typeof stringOrNumber !== "number") {
>"number" : string

stringOrNumber;
>stringOrNumber : {}
>stringOrNumber : nothing
}

6 changes: 3 additions & 3 deletions tests/baselines/reference/typeGuardTypeOfUndefined.types
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ function test2(a: any) {
>"boolean" : string

a;
>a : {}
>a : nothing
}
else {
a;
Expand Down Expand Up @@ -129,7 +129,7 @@ function test5(a: boolean | void) {
}
else {
a;
>a : {}
>a : nothing
}
}
else {
Expand Down Expand Up @@ -188,7 +188,7 @@ function test7(a: boolean | void) {
}
else {
a;
>a : {}
>a : nothing
}
}

Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/typeGuardsWithInstanceOf.errors.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOf.ts(7,20): error TS2339: Property 'global' does not exist on type '{}'.
tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOf.ts(7,20): error TS2339: Property 'global' does not exist on type 'nothing'.


==== tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOf.ts (1 errors) ====
Expand All @@ -10,5 +10,5 @@ tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOf.ts(7,20)
result = result2;
} else if (!result.global) {
~~~~~~
!!! error TS2339: Property 'global' does not exist on type '{}'.
!!! error TS2339: Property 'global' does not exist on type 'nothing'.
}