Skip to content

Commit e697e21

Browse files
committed
add tests
1 parent 43fab65 commit e697e21

File tree

46 files changed

+7752
-56
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+7752
-56
lines changed

tests/baselines/reference/discriminatedUnionTypes2.errors.txt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ tests/cases/conformance/types/union/discriminatedUnionTypes2.ts(27,30): error TS
22
Object literal may only specify known properties, and 'c' does not exist in type '{ a: null; b: string; }'.
33
tests/cases/conformance/types/union/discriminatedUnionTypes2.ts(32,11): error TS2339: Property 'b' does not exist on type '{ a: 0; b: string; } | { a: T; c: number; }'.
44
Property 'b' does not exist on type '{ a: T; c: number; }'.
5+
tests/cases/conformance/types/union/discriminatedUnionTypes2.ts(132,11): error TS2339: Property 'value' does not exist on type 'never'.
56

67

7-
==== tests/cases/conformance/types/union/discriminatedUnionTypes2.ts (2 errors) ====
8+
==== tests/cases/conformance/types/union/discriminatedUnionTypes2.ts (3 errors) ====
89
function f10(x : { kind: false, a: string } | { kind: true, b: string } | { kind: string, c: string }) {
910
if (x.kind === false) {
1011
x.a;
@@ -142,7 +143,9 @@ tests/cases/conformance/types/union/discriminatedUnionTypes2.ts(32,11): error TS
142143
x.value; // number
143144
}
144145
else {
145-
x.value; // number
146+
x.value; // Error, x is never
147+
~~~~~
148+
!!! error TS2339: Property 'value' does not exist on type 'never'.
146149
}
147150
}
148151

tests/baselines/reference/discriminatedUnionTypes2.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ function foo1(x: RuntimeValue & { type: 'number' }) {
130130
x.value; // number
131131
}
132132
else {
133-
x.value; // number
133+
x.value; // Error, x is never
134134
}
135135
}
136136

@@ -226,7 +226,7 @@ function foo1(x) {
226226
x.value; // number
227227
}
228228
else {
229-
x.value; // number
229+
x.value; // Error, x is never
230230
}
231231
}
232232
function foo2(x) {

tests/baselines/reference/discriminatedUnionTypes2.symbols

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -366,10 +366,8 @@ function foo1(x: RuntimeValue & { type: 'number' }) {
366366
>value : Symbol(value, Decl(discriminatedUnionTypes2.ts, 122, 23))
367367
}
368368
else {
369-
x.value; // number
370-
>x.value : Symbol(value, Decl(discriminatedUnionTypes2.ts, 122, 23))
369+
x.value; // Error, x is never
371370
>x : Symbol(x, Decl(discriminatedUnionTypes2.ts, 126, 14))
372-
>value : Symbol(value, Decl(discriminatedUnionTypes2.ts, 122, 23))
373371
}
374372
}
375373

tests/baselines/reference/discriminatedUnionTypes2.types

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -379,10 +379,10 @@ function foo1(x: RuntimeValue & { type: 'number' }) {
379379
>value : number
380380
}
381381
else {
382-
x.value; // number
383-
>x.value : number
384-
>x : { type: "number"; value: number; } & { type: "number"; }
385-
>value : number
382+
x.value; // Error, x is never
383+
>x.value : any
384+
>x : never
385+
>value : any
386386
}
387387
}
388388

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
tests/cases/conformance/types/union/discriminatedUnionTypes3.ts(59,11): error TS2339: Property 's' does not exist on type 'T'.
2+
3+
4+
==== tests/cases/conformance/types/union/discriminatedUnionTypes3.ts (1 errors) ====
5+
type A = { type2: "a", a: number }
6+
type B = { type2: "b", b: number }
7+
type C = { type2: "c", b: number | string }
8+
9+
type X = { type1: A, x: string }
10+
type Y = { type1: B, y: string }
11+
type Z = { type1: C, z: string }
12+
type W = { type1: undefined }
13+
14+
function f32(x: X | Y) {
15+
switch (x.type1.type2) {
16+
case "a":
17+
x.x // typeof x is X
18+
break;
19+
case "b":
20+
x.y // typeof x is Y
21+
break;
22+
}
23+
}
24+
25+
function f33(x: A | B) {
26+
switch (x.type2) {
27+
case "a":
28+
x // typeof x is X
29+
break;
30+
case "b":
31+
x // typeof x is Y
32+
break;
33+
}
34+
}
35+
36+
function f34(x: X | Y) {
37+
if (x.type1.type2 === "a") {
38+
x.x // typeof x is X
39+
} else if (x.type1.type2 === "b") {
40+
x.y // typeof x is Y
41+
}
42+
}
43+
44+
function f35(x: X | W) {
45+
if (x.type1?.type2 === "a") {
46+
x.x
47+
}
48+
}
49+
50+
function f36(x: X | W) {
51+
x.type1?.type2 ?? x;
52+
}
53+
54+
type S = { sub: { type0: X }, s: string }
55+
type T = { sub: { type0: Y }, t: string }
56+
57+
function f37(s: S | T) {
58+
if (s.sub.type0.type1.type2 === "a") {
59+
s.s // typeof s is S
60+
s.sub.type0.x // type of s.sub.type is X
61+
s.sub.type0.type1.a // type of s.sub.type.type is A
62+
} else {
63+
s.s // type error!
64+
~
65+
!!! error TS2339: Property 's' does not exist on type 'T'.
66+
}
67+
}
68+

tests/baselines/reference/discriminatedUnionTypes3.js

Lines changed: 112 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,66 @@
11
//// [discriminatedUnionTypes3.ts]
2+
type A = { type2: "a", a: number }
3+
type B = { type2: "b", b: number }
4+
type C = { type2: "c", b: number | string }
5+
6+
type X = { type1: A, x: string }
7+
type Y = { type1: B, y: string }
8+
type Z = { type1: C, z: string }
9+
type W = { type1: undefined }
10+
11+
function f32(x: X | Y) {
12+
switch (x.type1.type2) {
13+
case "a":
14+
x.x // typeof x is X
15+
break;
16+
case "b":
17+
x.y // typeof x is Y
18+
break;
19+
}
20+
}
21+
22+
function f33(x: A | B) {
23+
switch (x.type2) {
24+
case "a":
25+
x // typeof x is X
26+
break;
27+
case "b":
28+
x // typeof x is Y
29+
break;
30+
}
31+
}
32+
33+
function f34(x: X | Y) {
34+
if (x.type1.type2 === "a") {
35+
x.x // typeof x is X
36+
} else if (x.type1.type2 === "b") {
37+
x.y // typeof x is Y
38+
}
39+
}
40+
41+
function f35(x: X | W) {
42+
if (x.type1?.type2 === "a") {
43+
x.x
44+
}
45+
}
46+
47+
function f36(x: X | W) {
48+
x.type1?.type2 ?? x;
49+
}
50+
51+
type S = { sub: { type0: X }, s: string }
52+
type T = { sub: { type0: Y }, t: string }
53+
54+
function f37(s: S | T) {
55+
if (s.sub.type0.type1.type2 === "a") {
56+
s.s // typeof s is S
57+
s.sub.type0.x // type of s.sub.type is X
58+
s.sub.type0.type1.a // type of s.sub.type.type is A
59+
} else {
60+
s.s // type error!
61+
}
62+
}
63+
264
// Repro from #44435
365

466
type Correct = {
@@ -15,11 +77,59 @@ const example: SomeReturnType = {} as SomeReturnType;
1577

1678
if (example.err === undefined) {
1779
example.property; // true
18-
}
80+
}
81+
1982

2083
//// [discriminatedUnionTypes3.js]
2184
"use strict";
22-
// Repro from #44435
85+
function f32(x) {
86+
switch (x.type1.type2) {
87+
case "a":
88+
x.x; // typeof x is X
89+
break;
90+
case "b":
91+
x.y; // typeof x is Y
92+
break;
93+
}
94+
}
95+
function f33(x) {
96+
switch (x.type2) {
97+
case "a":
98+
x; // typeof x is X
99+
break;
100+
case "b":
101+
x; // typeof x is Y
102+
break;
103+
}
104+
}
105+
function f34(x) {
106+
if (x.type1.type2 === "a") {
107+
x.x; // typeof x is X
108+
}
109+
else if (x.type1.type2 === "b") {
110+
x.y; // typeof x is Y
111+
}
112+
}
113+
function f35(x) {
114+
var _a;
115+
if (((_a = x.type1) === null || _a === void 0 ? void 0 : _a.type2) === "a") {
116+
x.x;
117+
}
118+
}
119+
function f36(x) {
120+
var _a, _b;
121+
(_b = (_a = x.type1) === null || _a === void 0 ? void 0 : _a.type2) !== null && _b !== void 0 ? _b : x;
122+
}
123+
function f37(s) {
124+
if (s.sub.type0.type1.type2 === "a") {
125+
s.s; // typeof s is S
126+
s.sub.type0.x; // type of s.sub.type is X
127+
s.sub.type0.type1.a; // type of s.sub.type.type is A
128+
}
129+
else {
130+
s.s; // type error!
131+
}
132+
}
23133
var example = {};
24134
if (example.err === undefined) {
25135
example.property; // true

0 commit comments

Comments
 (0)