Skip to content

Commit 0613ad1

Browse files
committed
Merge pull request #8340 from Microsoft/nothingType
Display 'nothing' for the empty union type
2 parents 70d805a + 9da4323 commit 0613ad1

12 files changed

+32
-25
lines changed

src/compiler/checker.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -2014,7 +2014,12 @@ namespace ts {
20142014
writeUnionOrIntersectionType(<UnionOrIntersectionType>type, flags);
20152015
}
20162016
else if (type.flags & TypeFlags.Anonymous) {
2017-
writeAnonymousType(<ObjectType>type, flags);
2017+
if (type === emptyUnionType) {
2018+
writer.writeKeyword("nothing");
2019+
}
2020+
else {
2021+
writeAnonymousType(<ObjectType>type, flags);
2022+
}
20182023
}
20192024
else if (type.flags & TypeFlags.StringLiteral) {
20202025
writer.writeStringLiteral(`"${escapeString((<StringLiteralType>type).text)}"`);
@@ -4247,7 +4252,9 @@ namespace ts {
42474252
propTypes.push(getTypeOfSymbol(prop));
42484253
}
42494254
}
4250-
return getUnionType(propTypes);
4255+
if (propTypes.length) {
4256+
return getUnionType(propTypes);
4257+
}
42514258
}
42524259
return undefined;
42534260
}

tests/baselines/reference/instanceOfAssignability.types

+2-2
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,8 @@ function fn5(x: Derived1) {
133133
// 1.5: y: Derived1
134134
// Want: ???
135135
let y = x;
136-
>y : {}
137-
>x : {}
136+
>y : nothing
137+
>x : nothing
138138
}
139139
}
140140

tests/baselines/reference/stringLiteralTypesAsTags01.types

+2-2
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,6 @@ if (!hasKind(x, "B")) {
116116
}
117117
else {
118118
let d = x;
119-
>d : {}
120-
>x : {}
119+
>d : nothing
120+
>x : nothing
121121
}

tests/baselines/reference/stringLiteralTypesAsTags02.types

+2-2
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,6 @@ if (!hasKind(x, "B")) {
110110
}
111111
else {
112112
let d = x;
113-
>d : {}
114-
>x : {}
113+
>d : nothing
114+
>x : nothing
115115
}

tests/baselines/reference/stringLiteralTypesAsTags03.types

+2-2
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,6 @@ if (!hasKind(x, "B")) {
113113
}
114114
else {
115115
let d = x;
116-
>d : {}
117-
>x : {}
116+
>d : nothing
117+
>x : nothing
118118
}

tests/baselines/reference/typeGuardOfFormTypeOfBoolean.types

+2-2
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ if (typeof strOrNum === "boolean") {
121121

122122
let z1: {} = strOrNum; // {}
123123
>z1 : {}
124-
>strOrNum : {}
124+
>strOrNum : nothing
125125
}
126126
else {
127127
let z2: string | number = strOrNum; // string | number
@@ -215,6 +215,6 @@ if (typeof strOrNum !== "boolean") {
215215
else {
216216
let z2: {} = strOrNum; // {}
217217
>z2 : {}
218-
>strOrNum : {}
218+
>strOrNum : nothing
219219
}
220220

tests/baselines/reference/typeGuardOfFormTypeOfNumber.types

+2-2
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ if (typeof strOrBool === "number") {
120120

121121
let y1: {} = strOrBool; // {}
122122
>y1 : {}
123-
>strOrBool : {}
123+
>strOrBool : nothing
124124
}
125125
else {
126126
let y2: string | boolean = strOrBool; // string | boolean
@@ -212,6 +212,6 @@ if (typeof strOrBool !== "number") {
212212
else {
213213
let y2: {} = strOrBool; // {}
214214
>y2 : {}
215-
>strOrBool : {}
215+
>strOrBool : nothing
216216
}
217217

tests/baselines/reference/typeGuardOfFormTypeOfOther.types

+2-2
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ if (typeof strOrNumOrBool === "Object") {
105105

106106
let q1: {} = strOrNumOrBool; // {}
107107
>q1 : {}
108-
>strOrNumOrBool : {}
108+
>strOrNumOrBool : nothing
109109
}
110110
else {
111111
let q2: string | number | boolean = strOrNumOrBool; // string | number | boolean
@@ -178,6 +178,6 @@ if (typeof strOrNumOrBool !== "Object") {
178178
else {
179179
let q2: {} = strOrNumOrBool; // {}
180180
>q2 : {}
181-
>strOrNumOrBool : {}
181+
>strOrNumOrBool : nothing
182182
}
183183

tests/baselines/reference/typeGuardOfFormTypeOfString.types

+2-2
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ if (typeof numOrBool === "string") {
121121

122122
let x1: {} = numOrBool; // {}
123123
>x1 : {}
124-
>numOrBool : {}
124+
>numOrBool : nothing
125125
}
126126
else {
127127
let x2: number | boolean = numOrBool; // number | boolean
@@ -214,6 +214,6 @@ if (typeof numOrBool !== "string") {
214214
else {
215215
let x2: {} = numOrBool; // {}
216216
>x2 : {}
217-
>numOrBool : {}
217+
>numOrBool : nothing
218218
}
219219

tests/baselines/reference/typeGuardTautologicalConsistiency.types

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ if (typeof stringOrNumber === "number") {
1515
>"number" : string
1616

1717
stringOrNumber;
18-
>stringOrNumber : {}
18+
>stringOrNumber : nothing
1919
}
2020
}
2121

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

3333
stringOrNumber;
34-
>stringOrNumber : {}
34+
>stringOrNumber : nothing
3535
}
3636

tests/baselines/reference/typeGuardTypeOfUndefined.types

+3-3
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ function test2(a: any) {
4747
>"boolean" : string
4848

4949
a;
50-
>a : {}
50+
>a : nothing
5151
}
5252
else {
5353
a;
@@ -129,7 +129,7 @@ function test5(a: boolean | void) {
129129
}
130130
else {
131131
a;
132-
>a : {}
132+
>a : nothing
133133
}
134134
}
135135
else {
@@ -188,7 +188,7 @@ function test7(a: boolean | void) {
188188
}
189189
else {
190190
a;
191-
>a : {}
191+
>a : nothing
192192
}
193193
}
194194

tests/baselines/reference/typeGuardsWithInstanceOf.errors.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOf.ts(7,20): error TS2339: Property 'global' does not exist on type '{}'.
1+
tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOf.ts(7,20): error TS2339: Property 'global' does not exist on type 'nothing'.
22

33

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

0 commit comments

Comments
 (0)