Skip to content

Commit 22907bf

Browse files
committed
Add tests
1 parent 7737cd5 commit 22907bf

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// @strict: true
2+
3+
function f10(x : { kind: false, a: string } | { kind: true, b: string } | { kind: string, c: string }) {
4+
if (x.kind === false) {
5+
x.a;
6+
}
7+
else if (x.kind === true) {
8+
x.b;
9+
}
10+
else {
11+
x.c;
12+
}
13+
}
14+
15+
function f11(x : { kind: false, a: string } | { kind: true, b: string } | { kind: string, c: string }) {
16+
switch (x.kind) {
17+
case false:
18+
x.a;
19+
break;
20+
case true:
21+
x.b;
22+
break;
23+
default:
24+
x.c;
25+
}
26+
}
27+
28+
function f13(x: { a: null; b: string } | { a: string, c: number }) {
29+
x = { a: null, b: "foo", c: 4}; // Error
30+
}
31+
32+
function f14<T>(x: { a: 0; b: string } | { a: T, c: number }) {
33+
if (x.a === 0) {
34+
x.b; // Error
35+
}
36+
}
37+
38+
type Result<T> = { error?: undefined, value: T } | { error: Error };
39+
40+
function f15(x: Result<number>) {
41+
if (!x.error) {
42+
x.value;
43+
}
44+
else {
45+
x.error.message;
46+
}
47+
}
48+
49+
f15({ value: 10 });
50+
f15({ error: new Error("boom") });
51+
52+
// Repro from #24193
53+
54+
interface WithError {
55+
error: Error
56+
data: null
57+
}
58+
59+
interface WithoutError<Data> {
60+
error: null
61+
data: Data
62+
}
63+
64+
type DataCarrier<Data> = WithError | WithoutError<Data>
65+
66+
function f20<Data>(carrier: DataCarrier<Data>) {
67+
if (carrier.error === null) {
68+
const error: null = carrier.error
69+
const data: Data = carrier.data
70+
} else {
71+
const error: Error = carrier.error
72+
const data: null = carrier.data
73+
}
74+
}

0 commit comments

Comments
 (0)