Skip to content

Commit 29ae2b2

Browse files
committed
Add regression tests
1 parent b336c69 commit 29ae2b2

File tree

4 files changed

+391
-0
lines changed

4 files changed

+391
-0
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
//// [discriminantsAndPrimitives.ts]
2+
3+
// Repro from #10257 plus other tests
4+
5+
interface Foo {
6+
kind: "foo";
7+
name: string;
8+
}
9+
10+
interface Bar {
11+
kind: "bar";
12+
length: string;
13+
}
14+
15+
function f1(x: Foo | Bar | string) {
16+
if (typeof x !== 'string') {
17+
switch(x.kind) {
18+
case 'foo':
19+
x.name;
20+
}
21+
}
22+
}
23+
24+
function f2(x: Foo | Bar | string | undefined) {
25+
if (typeof x === "object") {
26+
switch(x.kind) {
27+
case 'foo':
28+
x.name;
29+
}
30+
}
31+
}
32+
33+
function f3(x: Foo | Bar | string | null) {
34+
if (x && typeof x !== "string") {
35+
switch(x.kind) {
36+
case 'foo':
37+
x.name;
38+
}
39+
}
40+
}
41+
42+
function f4(x: Foo | Bar | string | number | null) {
43+
if (x && typeof x === "object") {
44+
switch(x.kind) {
45+
case 'foo':
46+
x.name;
47+
}
48+
}
49+
}
50+
51+
//// [discriminantsAndPrimitives.js]
52+
// Repro from #10257 plus other tests
53+
function f1(x) {
54+
if (typeof x !== 'string') {
55+
switch (x.kind) {
56+
case 'foo':
57+
x.name;
58+
}
59+
}
60+
}
61+
function f2(x) {
62+
if (typeof x === "object") {
63+
switch (x.kind) {
64+
case 'foo':
65+
x.name;
66+
}
67+
}
68+
}
69+
function f3(x) {
70+
if (x && typeof x !== "string") {
71+
switch (x.kind) {
72+
case 'foo':
73+
x.name;
74+
}
75+
}
76+
}
77+
function f4(x) {
78+
if (x && typeof x === "object") {
79+
switch (x.kind) {
80+
case 'foo':
81+
x.name;
82+
}
83+
}
84+
}
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
=== tests/cases/compiler/discriminantsAndPrimitives.ts ===
2+
3+
// Repro from #10257 plus other tests
4+
5+
interface Foo {
6+
>Foo : Symbol(Foo, Decl(discriminantsAndPrimitives.ts, 0, 0))
7+
8+
kind: "foo";
9+
>kind : Symbol(Foo.kind, Decl(discriminantsAndPrimitives.ts, 3, 15))
10+
11+
name: string;
12+
>name : Symbol(Foo.name, Decl(discriminantsAndPrimitives.ts, 4, 16))
13+
}
14+
15+
interface Bar {
16+
>Bar : Symbol(Bar, Decl(discriminantsAndPrimitives.ts, 6, 1))
17+
18+
kind: "bar";
19+
>kind : Symbol(Bar.kind, Decl(discriminantsAndPrimitives.ts, 8, 15))
20+
21+
length: string;
22+
>length : Symbol(Bar.length, Decl(discriminantsAndPrimitives.ts, 9, 16))
23+
}
24+
25+
function f1(x: Foo | Bar | string) {
26+
>f1 : Symbol(f1, Decl(discriminantsAndPrimitives.ts, 11, 1))
27+
>x : Symbol(x, Decl(discriminantsAndPrimitives.ts, 13, 12))
28+
>Foo : Symbol(Foo, Decl(discriminantsAndPrimitives.ts, 0, 0))
29+
>Bar : Symbol(Bar, Decl(discriminantsAndPrimitives.ts, 6, 1))
30+
31+
if (typeof x !== 'string') {
32+
>x : Symbol(x, Decl(discriminantsAndPrimitives.ts, 13, 12))
33+
34+
switch(x.kind) {
35+
>x.kind : Symbol(kind, Decl(discriminantsAndPrimitives.ts, 3, 15), Decl(discriminantsAndPrimitives.ts, 8, 15))
36+
>x : Symbol(x, Decl(discriminantsAndPrimitives.ts, 13, 12))
37+
>kind : Symbol(kind, Decl(discriminantsAndPrimitives.ts, 3, 15), Decl(discriminantsAndPrimitives.ts, 8, 15))
38+
39+
case 'foo':
40+
x.name;
41+
>x.name : Symbol(Foo.name, Decl(discriminantsAndPrimitives.ts, 4, 16))
42+
>x : Symbol(x, Decl(discriminantsAndPrimitives.ts, 13, 12))
43+
>name : Symbol(Foo.name, Decl(discriminantsAndPrimitives.ts, 4, 16))
44+
}
45+
}
46+
}
47+
48+
function f2(x: Foo | Bar | string | undefined) {
49+
>f2 : Symbol(f2, Decl(discriminantsAndPrimitives.ts, 20, 1))
50+
>x : Symbol(x, Decl(discriminantsAndPrimitives.ts, 22, 12))
51+
>Foo : Symbol(Foo, Decl(discriminantsAndPrimitives.ts, 0, 0))
52+
>Bar : Symbol(Bar, Decl(discriminantsAndPrimitives.ts, 6, 1))
53+
54+
if (typeof x === "object") {
55+
>x : Symbol(x, Decl(discriminantsAndPrimitives.ts, 22, 12))
56+
57+
switch(x.kind) {
58+
>x.kind : Symbol(kind, Decl(discriminantsAndPrimitives.ts, 3, 15), Decl(discriminantsAndPrimitives.ts, 8, 15))
59+
>x : Symbol(x, Decl(discriminantsAndPrimitives.ts, 22, 12))
60+
>kind : Symbol(kind, Decl(discriminantsAndPrimitives.ts, 3, 15), Decl(discriminantsAndPrimitives.ts, 8, 15))
61+
62+
case 'foo':
63+
x.name;
64+
>x.name : Symbol(Foo.name, Decl(discriminantsAndPrimitives.ts, 4, 16))
65+
>x : Symbol(x, Decl(discriminantsAndPrimitives.ts, 22, 12))
66+
>name : Symbol(Foo.name, Decl(discriminantsAndPrimitives.ts, 4, 16))
67+
}
68+
}
69+
}
70+
71+
function f3(x: Foo | Bar | string | null) {
72+
>f3 : Symbol(f3, Decl(discriminantsAndPrimitives.ts, 29, 1))
73+
>x : Symbol(x, Decl(discriminantsAndPrimitives.ts, 31, 12))
74+
>Foo : Symbol(Foo, Decl(discriminantsAndPrimitives.ts, 0, 0))
75+
>Bar : Symbol(Bar, Decl(discriminantsAndPrimitives.ts, 6, 1))
76+
77+
if (x && typeof x !== "string") {
78+
>x : Symbol(x, Decl(discriminantsAndPrimitives.ts, 31, 12))
79+
>x : Symbol(x, Decl(discriminantsAndPrimitives.ts, 31, 12))
80+
81+
switch(x.kind) {
82+
>x.kind : Symbol(kind, Decl(discriminantsAndPrimitives.ts, 3, 15), Decl(discriminantsAndPrimitives.ts, 8, 15))
83+
>x : Symbol(x, Decl(discriminantsAndPrimitives.ts, 31, 12))
84+
>kind : Symbol(kind, Decl(discriminantsAndPrimitives.ts, 3, 15), Decl(discriminantsAndPrimitives.ts, 8, 15))
85+
86+
case 'foo':
87+
x.name;
88+
>x.name : Symbol(Foo.name, Decl(discriminantsAndPrimitives.ts, 4, 16))
89+
>x : Symbol(x, Decl(discriminantsAndPrimitives.ts, 31, 12))
90+
>name : Symbol(Foo.name, Decl(discriminantsAndPrimitives.ts, 4, 16))
91+
}
92+
}
93+
}
94+
95+
function f4(x: Foo | Bar | string | number | null) {
96+
>f4 : Symbol(f4, Decl(discriminantsAndPrimitives.ts, 38, 1))
97+
>x : Symbol(x, Decl(discriminantsAndPrimitives.ts, 40, 12))
98+
>Foo : Symbol(Foo, Decl(discriminantsAndPrimitives.ts, 0, 0))
99+
>Bar : Symbol(Bar, Decl(discriminantsAndPrimitives.ts, 6, 1))
100+
101+
if (x && typeof x === "object") {
102+
>x : Symbol(x, Decl(discriminantsAndPrimitives.ts, 40, 12))
103+
>x : Symbol(x, Decl(discriminantsAndPrimitives.ts, 40, 12))
104+
105+
switch(x.kind) {
106+
>x.kind : Symbol(kind, Decl(discriminantsAndPrimitives.ts, 3, 15), Decl(discriminantsAndPrimitives.ts, 8, 15))
107+
>x : Symbol(x, Decl(discriminantsAndPrimitives.ts, 40, 12))
108+
>kind : Symbol(kind, Decl(discriminantsAndPrimitives.ts, 3, 15), Decl(discriminantsAndPrimitives.ts, 8, 15))
109+
110+
case 'foo':
111+
x.name;
112+
>x.name : Symbol(Foo.name, Decl(discriminantsAndPrimitives.ts, 4, 16))
113+
>x : Symbol(x, Decl(discriminantsAndPrimitives.ts, 40, 12))
114+
>name : Symbol(Foo.name, Decl(discriminantsAndPrimitives.ts, 4, 16))
115+
}
116+
}
117+
}
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
=== tests/cases/compiler/discriminantsAndPrimitives.ts ===
2+
3+
// Repro from #10257 plus other tests
4+
5+
interface Foo {
6+
>Foo : Foo
7+
8+
kind: "foo";
9+
>kind : "foo"
10+
11+
name: string;
12+
>name : string
13+
}
14+
15+
interface Bar {
16+
>Bar : Bar
17+
18+
kind: "bar";
19+
>kind : "bar"
20+
21+
length: string;
22+
>length : string
23+
}
24+
25+
function f1(x: Foo | Bar | string) {
26+
>f1 : (x: string | Foo | Bar) => void
27+
>x : string | Foo | Bar
28+
>Foo : Foo
29+
>Bar : Bar
30+
31+
if (typeof x !== 'string') {
32+
>typeof x !== 'string' : boolean
33+
>typeof x : string
34+
>x : string | Foo | Bar
35+
>'string' : "string"
36+
37+
switch(x.kind) {
38+
>x.kind : "foo" | "bar"
39+
>x : Foo | Bar
40+
>kind : "foo" | "bar"
41+
42+
case 'foo':
43+
>'foo' : "foo"
44+
45+
x.name;
46+
>x.name : string
47+
>x : Foo
48+
>name : string
49+
}
50+
}
51+
}
52+
53+
function f2(x: Foo | Bar | string | undefined) {
54+
>f2 : (x: string | Foo | Bar | undefined) => void
55+
>x : string | Foo | Bar | undefined
56+
>Foo : Foo
57+
>Bar : Bar
58+
59+
if (typeof x === "object") {
60+
>typeof x === "object" : boolean
61+
>typeof x : string
62+
>x : string | Foo | Bar | undefined
63+
>"object" : "object"
64+
65+
switch(x.kind) {
66+
>x.kind : "foo" | "bar"
67+
>x : Foo | Bar
68+
>kind : "foo" | "bar"
69+
70+
case 'foo':
71+
>'foo' : "foo"
72+
73+
x.name;
74+
>x.name : string
75+
>x : Foo
76+
>name : string
77+
}
78+
}
79+
}
80+
81+
function f3(x: Foo | Bar | string | null) {
82+
>f3 : (x: string | Foo | Bar | null) => void
83+
>x : string | Foo | Bar | null
84+
>Foo : Foo
85+
>Bar : Bar
86+
>null : null
87+
88+
if (x && typeof x !== "string") {
89+
>x && typeof x !== "string" : boolean | "" | null
90+
>x : string | Foo | Bar | null
91+
>typeof x !== "string" : boolean
92+
>typeof x : string
93+
>x : string | Foo | Bar
94+
>"string" : "string"
95+
96+
switch(x.kind) {
97+
>x.kind : "foo" | "bar"
98+
>x : Foo | Bar
99+
>kind : "foo" | "bar"
100+
101+
case 'foo':
102+
>'foo' : "foo"
103+
104+
x.name;
105+
>x.name : string
106+
>x : Foo
107+
>name : string
108+
}
109+
}
110+
}
111+
112+
function f4(x: Foo | Bar | string | number | null) {
113+
>f4 : (x: string | number | Foo | Bar | null) => void
114+
>x : string | number | Foo | Bar | null
115+
>Foo : Foo
116+
>Bar : Bar
117+
>null : null
118+
119+
if (x && typeof x === "object") {
120+
>x && typeof x === "object" : boolean | "" | 0 | null
121+
>x : string | number | Foo | Bar | null
122+
>typeof x === "object" : boolean
123+
>typeof x : string
124+
>x : string | number | Foo | Bar
125+
>"object" : "object"
126+
127+
switch(x.kind) {
128+
>x.kind : "foo" | "bar"
129+
>x : Foo | Bar
130+
>kind : "foo" | "bar"
131+
132+
case 'foo':
133+
>'foo' : "foo"
134+
135+
x.name;
136+
>x.name : string
137+
>x : Foo
138+
>name : string
139+
}
140+
}
141+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// @strictNullChecks: true
2+
3+
// Repro from #10257 plus other tests
4+
5+
interface Foo {
6+
kind: "foo";
7+
name: string;
8+
}
9+
10+
interface Bar {
11+
kind: "bar";
12+
length: string;
13+
}
14+
15+
function f1(x: Foo | Bar | string) {
16+
if (typeof x !== 'string') {
17+
switch(x.kind) {
18+
case 'foo':
19+
x.name;
20+
}
21+
}
22+
}
23+
24+
function f2(x: Foo | Bar | string | undefined) {
25+
if (typeof x === "object") {
26+
switch(x.kind) {
27+
case 'foo':
28+
x.name;
29+
}
30+
}
31+
}
32+
33+
function f3(x: Foo | Bar | string | null) {
34+
if (x && typeof x !== "string") {
35+
switch(x.kind) {
36+
case 'foo':
37+
x.name;
38+
}
39+
}
40+
}
41+
42+
function f4(x: Foo | Bar | string | number | null) {
43+
if (x && typeof x === "object") {
44+
switch(x.kind) {
45+
case 'foo':
46+
x.name;
47+
}
48+
}
49+
}

0 commit comments

Comments
 (0)