Skip to content

Commit 1375505

Browse files
committed
Add tests
1 parent 37e96b3 commit 1375505

File tree

3 files changed

+257
-0
lines changed

3 files changed

+257
-0
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
tests/cases/compiler/discriminantPropertyCheck.ts(30,9): error TS2532: Object is possibly 'undefined'.
2+
tests/cases/compiler/discriminantPropertyCheck.ts(66,9): error TS2532: Object is possibly 'undefined'.
3+
4+
5+
==== tests/cases/compiler/discriminantPropertyCheck.ts (2 errors) ====
6+
7+
type Item = Item1 | Item2;
8+
9+
interface Base {
10+
bar: boolean;
11+
}
12+
13+
interface Item1 extends Base {
14+
kind: "A";
15+
foo: string | undefined;
16+
baz: boolean;
17+
qux: true;
18+
}
19+
20+
interface Item2 extends Base {
21+
kind: "B";
22+
foo: string | undefined;
23+
baz: boolean;
24+
qux: false;
25+
}
26+
27+
function goo1(x: Item) {
28+
if (x.kind === "A" && x.foo !== undefined) {
29+
x.foo.length;
30+
}
31+
}
32+
33+
function goo2(x: Item) {
34+
if (x.foo !== undefined && x.kind === "A") {
35+
x.foo.length; // Error, intervening discriminant guard
36+
~~~~~
37+
!!! error TS2532: Object is possibly 'undefined'.
38+
}
39+
}
40+
41+
function foo1(x: Item) {
42+
if (x.bar && x.foo !== undefined) {
43+
x.foo.length;
44+
}
45+
}
46+
47+
function foo2(x: Item) {
48+
if (x.foo !== undefined && x.bar) {
49+
x.foo.length;
50+
}
51+
}
52+
53+
function foo3(x: Item) {
54+
if (x.baz && x.foo !== undefined) {
55+
x.foo.length;
56+
}
57+
}
58+
59+
function foo4(x: Item) {
60+
if (x.foo !== undefined && x.baz) {
61+
x.foo.length;
62+
}
63+
}
64+
65+
function foo5(x: Item) {
66+
if (x.qux && x.foo !== undefined) {
67+
x.foo.length;
68+
}
69+
}
70+
71+
function foo6(x: Item) {
72+
if (x.foo !== undefined && x.qux) {
73+
x.foo.length; // Error, intervening discriminant guard
74+
~~~~~
75+
!!! error TS2532: Object is possibly 'undefined'.
76+
}
77+
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
//// [discriminantPropertyCheck.ts]
2+
3+
type Item = Item1 | Item2;
4+
5+
interface Base {
6+
bar: boolean;
7+
}
8+
9+
interface Item1 extends Base {
10+
kind: "A";
11+
foo: string | undefined;
12+
baz: boolean;
13+
qux: true;
14+
}
15+
16+
interface Item2 extends Base {
17+
kind: "B";
18+
foo: string | undefined;
19+
baz: boolean;
20+
qux: false;
21+
}
22+
23+
function goo1(x: Item) {
24+
if (x.kind === "A" && x.foo !== undefined) {
25+
x.foo.length;
26+
}
27+
}
28+
29+
function goo2(x: Item) {
30+
if (x.foo !== undefined && x.kind === "A") {
31+
x.foo.length; // Error, intervening discriminant guard
32+
}
33+
}
34+
35+
function foo1(x: Item) {
36+
if (x.bar && x.foo !== undefined) {
37+
x.foo.length;
38+
}
39+
}
40+
41+
function foo2(x: Item) {
42+
if (x.foo !== undefined && x.bar) {
43+
x.foo.length;
44+
}
45+
}
46+
47+
function foo3(x: Item) {
48+
if (x.baz && x.foo !== undefined) {
49+
x.foo.length;
50+
}
51+
}
52+
53+
function foo4(x: Item) {
54+
if (x.foo !== undefined && x.baz) {
55+
x.foo.length;
56+
}
57+
}
58+
59+
function foo5(x: Item) {
60+
if (x.qux && x.foo !== undefined) {
61+
x.foo.length;
62+
}
63+
}
64+
65+
function foo6(x: Item) {
66+
if (x.foo !== undefined && x.qux) {
67+
x.foo.length; // Error, intervening discriminant guard
68+
}
69+
}
70+
71+
//// [discriminantPropertyCheck.js]
72+
function goo1(x) {
73+
if (x.kind === "A" && x.foo !== undefined) {
74+
x.foo.length;
75+
}
76+
}
77+
function goo2(x) {
78+
if (x.foo !== undefined && x.kind === "A") {
79+
x.foo.length; // Error, intervening discriminant guard
80+
}
81+
}
82+
function foo1(x) {
83+
if (x.bar && x.foo !== undefined) {
84+
x.foo.length;
85+
}
86+
}
87+
function foo2(x) {
88+
if (x.foo !== undefined && x.bar) {
89+
x.foo.length;
90+
}
91+
}
92+
function foo3(x) {
93+
if (x.baz && x.foo !== undefined) {
94+
x.foo.length;
95+
}
96+
}
97+
function foo4(x) {
98+
if (x.foo !== undefined && x.baz) {
99+
x.foo.length;
100+
}
101+
}
102+
function foo5(x) {
103+
if (x.qux && x.foo !== undefined) {
104+
x.foo.length;
105+
}
106+
}
107+
function foo6(x) {
108+
if (x.foo !== undefined && x.qux) {
109+
x.foo.length; // Error, intervening discriminant guard
110+
}
111+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// @strictNullChecks: true
2+
3+
type Item = Item1 | Item2;
4+
5+
interface Base {
6+
bar: boolean;
7+
}
8+
9+
interface Item1 extends Base {
10+
kind: "A";
11+
foo: string | undefined;
12+
baz: boolean;
13+
qux: true;
14+
}
15+
16+
interface Item2 extends Base {
17+
kind: "B";
18+
foo: string | undefined;
19+
baz: boolean;
20+
qux: false;
21+
}
22+
23+
function goo1(x: Item) {
24+
if (x.kind === "A" && x.foo !== undefined) {
25+
x.foo.length;
26+
}
27+
}
28+
29+
function goo2(x: Item) {
30+
if (x.foo !== undefined && x.kind === "A") {
31+
x.foo.length; // Error, intervening discriminant guard
32+
}
33+
}
34+
35+
function foo1(x: Item) {
36+
if (x.bar && x.foo !== undefined) {
37+
x.foo.length;
38+
}
39+
}
40+
41+
function foo2(x: Item) {
42+
if (x.foo !== undefined && x.bar) {
43+
x.foo.length;
44+
}
45+
}
46+
47+
function foo3(x: Item) {
48+
if (x.baz && x.foo !== undefined) {
49+
x.foo.length;
50+
}
51+
}
52+
53+
function foo4(x: Item) {
54+
if (x.foo !== undefined && x.baz) {
55+
x.foo.length;
56+
}
57+
}
58+
59+
function foo5(x: Item) {
60+
if (x.qux && x.foo !== undefined) {
61+
x.foo.length;
62+
}
63+
}
64+
65+
function foo6(x: Item) {
66+
if (x.foo !== undefined && x.qux) {
67+
x.foo.length; // Error, intervening discriminant guard
68+
}
69+
}

0 commit comments

Comments
 (0)