Skip to content

Commit 8b26ced

Browse files
committed
Add regression tests
1 parent 94a0daf commit 8b26ced

File tree

4 files changed

+368
-0
lines changed

4 files changed

+368
-0
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
//// [partiallyDiscriminantedUnions.ts]
2+
// Repro from #10586
3+
4+
interface A1 {
5+
type: 'a';
6+
subtype: 1;
7+
}
8+
9+
interface A2 {
10+
type: 'a';
11+
subtype: 2;
12+
foo: number;
13+
}
14+
15+
interface B {
16+
type: 'b';
17+
}
18+
19+
type AB = A1 | A2 | B;
20+
21+
const ab: AB = <AB>{};
22+
23+
if (ab.type === 'a') {
24+
if (ab.subtype === 2) {
25+
ab.foo;
26+
}
27+
}
28+
29+
// Repro from #11185
30+
31+
class Square { kind: "square"; }
32+
class Circle { kind: "circle"; }
33+
34+
type Shape = Circle | Square;
35+
type Shapes = Shape | Array<Shape>;
36+
37+
function isShape(s : Shapes): s is Shape {
38+
return !Array.isArray(s);
39+
}
40+
41+
function fail(s: Shapes) {
42+
if (isShape(s)) {
43+
if (s.kind === "circle") {
44+
let c: Circle = s;
45+
}
46+
}
47+
}
48+
49+
//// [partiallyDiscriminantedUnions.js]
50+
// Repro from #10586
51+
var ab = {};
52+
if (ab.type === 'a') {
53+
if (ab.subtype === 2) {
54+
ab.foo;
55+
}
56+
}
57+
// Repro from #11185
58+
var Square = (function () {
59+
function Square() {
60+
}
61+
return Square;
62+
}());
63+
var Circle = (function () {
64+
function Circle() {
65+
}
66+
return Circle;
67+
}());
68+
function isShape(s) {
69+
return !Array.isArray(s);
70+
}
71+
function fail(s) {
72+
if (isShape(s)) {
73+
if (s.kind === "circle") {
74+
var c = s;
75+
}
76+
}
77+
}
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
=== tests/cases/compiler/partiallyDiscriminantedUnions.ts ===
2+
// Repro from #10586
3+
4+
interface A1 {
5+
>A1 : Symbol(A1, Decl(partiallyDiscriminantedUnions.ts, 0, 0))
6+
7+
type: 'a';
8+
>type : Symbol(A1.type, Decl(partiallyDiscriminantedUnions.ts, 2, 14))
9+
10+
subtype: 1;
11+
>subtype : Symbol(A1.subtype, Decl(partiallyDiscriminantedUnions.ts, 3, 14))
12+
}
13+
14+
interface A2 {
15+
>A2 : Symbol(A2, Decl(partiallyDiscriminantedUnions.ts, 5, 1))
16+
17+
type: 'a';
18+
>type : Symbol(A2.type, Decl(partiallyDiscriminantedUnions.ts, 7, 14))
19+
20+
subtype: 2;
21+
>subtype : Symbol(A2.subtype, Decl(partiallyDiscriminantedUnions.ts, 8, 14))
22+
23+
foo: number;
24+
>foo : Symbol(A2.foo, Decl(partiallyDiscriminantedUnions.ts, 9, 15))
25+
}
26+
27+
interface B {
28+
>B : Symbol(B, Decl(partiallyDiscriminantedUnions.ts, 11, 1))
29+
30+
type: 'b';
31+
>type : Symbol(B.type, Decl(partiallyDiscriminantedUnions.ts, 13, 13))
32+
}
33+
34+
type AB = A1 | A2 | B;
35+
>AB : Symbol(AB, Decl(partiallyDiscriminantedUnions.ts, 15, 1))
36+
>A1 : Symbol(A1, Decl(partiallyDiscriminantedUnions.ts, 0, 0))
37+
>A2 : Symbol(A2, Decl(partiallyDiscriminantedUnions.ts, 5, 1))
38+
>B : Symbol(B, Decl(partiallyDiscriminantedUnions.ts, 11, 1))
39+
40+
const ab: AB = <AB>{};
41+
>ab : Symbol(ab, Decl(partiallyDiscriminantedUnions.ts, 19, 5))
42+
>AB : Symbol(AB, Decl(partiallyDiscriminantedUnions.ts, 15, 1))
43+
>AB : Symbol(AB, Decl(partiallyDiscriminantedUnions.ts, 15, 1))
44+
45+
if (ab.type === 'a') {
46+
>ab.type : Symbol(type, Decl(partiallyDiscriminantedUnions.ts, 2, 14), Decl(partiallyDiscriminantedUnions.ts, 7, 14), Decl(partiallyDiscriminantedUnions.ts, 13, 13))
47+
>ab : Symbol(ab, Decl(partiallyDiscriminantedUnions.ts, 19, 5))
48+
>type : Symbol(type, Decl(partiallyDiscriminantedUnions.ts, 2, 14), Decl(partiallyDiscriminantedUnions.ts, 7, 14), Decl(partiallyDiscriminantedUnions.ts, 13, 13))
49+
50+
if (ab.subtype === 2) {
51+
>ab.subtype : Symbol(subtype, Decl(partiallyDiscriminantedUnions.ts, 3, 14), Decl(partiallyDiscriminantedUnions.ts, 8, 14))
52+
>ab : Symbol(ab, Decl(partiallyDiscriminantedUnions.ts, 19, 5))
53+
>subtype : Symbol(subtype, Decl(partiallyDiscriminantedUnions.ts, 3, 14), Decl(partiallyDiscriminantedUnions.ts, 8, 14))
54+
55+
ab.foo;
56+
>ab.foo : Symbol(A2.foo, Decl(partiallyDiscriminantedUnions.ts, 9, 15))
57+
>ab : Symbol(ab, Decl(partiallyDiscriminantedUnions.ts, 19, 5))
58+
>foo : Symbol(A2.foo, Decl(partiallyDiscriminantedUnions.ts, 9, 15))
59+
}
60+
}
61+
62+
// Repro from #11185
63+
64+
class Square { kind: "square"; }
65+
>Square : Symbol(Square, Decl(partiallyDiscriminantedUnions.ts, 25, 1))
66+
>kind : Symbol(Square.kind, Decl(partiallyDiscriminantedUnions.ts, 29, 14))
67+
68+
class Circle { kind: "circle"; }
69+
>Circle : Symbol(Circle, Decl(partiallyDiscriminantedUnions.ts, 29, 32))
70+
>kind : Symbol(Circle.kind, Decl(partiallyDiscriminantedUnions.ts, 30, 14))
71+
72+
type Shape = Circle | Square;
73+
>Shape : Symbol(Shape, Decl(partiallyDiscriminantedUnions.ts, 30, 32))
74+
>Circle : Symbol(Circle, Decl(partiallyDiscriminantedUnions.ts, 29, 32))
75+
>Square : Symbol(Square, Decl(partiallyDiscriminantedUnions.ts, 25, 1))
76+
77+
type Shapes = Shape | Array<Shape>;
78+
>Shapes : Symbol(Shapes, Decl(partiallyDiscriminantedUnions.ts, 32, 29))
79+
>Shape : Symbol(Shape, Decl(partiallyDiscriminantedUnions.ts, 30, 32))
80+
>Array : Symbol(Array, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
81+
>Shape : Symbol(Shape, Decl(partiallyDiscriminantedUnions.ts, 30, 32))
82+
83+
function isShape(s : Shapes): s is Shape {
84+
>isShape : Symbol(isShape, Decl(partiallyDiscriminantedUnions.ts, 33, 35))
85+
>s : Symbol(s, Decl(partiallyDiscriminantedUnions.ts, 35, 17))
86+
>Shapes : Symbol(Shapes, Decl(partiallyDiscriminantedUnions.ts, 32, 29))
87+
>s : Symbol(s, Decl(partiallyDiscriminantedUnions.ts, 35, 17))
88+
>Shape : Symbol(Shape, Decl(partiallyDiscriminantedUnions.ts, 30, 32))
89+
90+
return !Array.isArray(s);
91+
>Array.isArray : Symbol(ArrayConstructor.isArray, Decl(lib.d.ts, --, --))
92+
>Array : Symbol(Array, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
93+
>isArray : Symbol(ArrayConstructor.isArray, Decl(lib.d.ts, --, --))
94+
>s : Symbol(s, Decl(partiallyDiscriminantedUnions.ts, 35, 17))
95+
}
96+
97+
function fail(s: Shapes) {
98+
>fail : Symbol(fail, Decl(partiallyDiscriminantedUnions.ts, 37, 1))
99+
>s : Symbol(s, Decl(partiallyDiscriminantedUnions.ts, 39, 14))
100+
>Shapes : Symbol(Shapes, Decl(partiallyDiscriminantedUnions.ts, 32, 29))
101+
102+
if (isShape(s)) {
103+
>isShape : Symbol(isShape, Decl(partiallyDiscriminantedUnions.ts, 33, 35))
104+
>s : Symbol(s, Decl(partiallyDiscriminantedUnions.ts, 39, 14))
105+
106+
if (s.kind === "circle") {
107+
>s.kind : Symbol(kind, Decl(partiallyDiscriminantedUnions.ts, 29, 14), Decl(partiallyDiscriminantedUnions.ts, 30, 14))
108+
>s : Symbol(s, Decl(partiallyDiscriminantedUnions.ts, 39, 14))
109+
>kind : Symbol(kind, Decl(partiallyDiscriminantedUnions.ts, 29, 14), Decl(partiallyDiscriminantedUnions.ts, 30, 14))
110+
111+
let c: Circle = s;
112+
>c : Symbol(c, Decl(partiallyDiscriminantedUnions.ts, 42, 15))
113+
>Circle : Symbol(Circle, Decl(partiallyDiscriminantedUnions.ts, 29, 32))
114+
>s : Symbol(s, Decl(partiallyDiscriminantedUnions.ts, 39, 14))
115+
}
116+
}
117+
}
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
=== tests/cases/compiler/partiallyDiscriminantedUnions.ts ===
2+
// Repro from #10586
3+
4+
interface A1 {
5+
>A1 : A1
6+
7+
type: 'a';
8+
>type : "a"
9+
10+
subtype: 1;
11+
>subtype : 1
12+
}
13+
14+
interface A2 {
15+
>A2 : A2
16+
17+
type: 'a';
18+
>type : "a"
19+
20+
subtype: 2;
21+
>subtype : 2
22+
23+
foo: number;
24+
>foo : number
25+
}
26+
27+
interface B {
28+
>B : B
29+
30+
type: 'b';
31+
>type : "b"
32+
}
33+
34+
type AB = A1 | A2 | B;
35+
>AB : AB
36+
>A1 : A1
37+
>A2 : A2
38+
>B : B
39+
40+
const ab: AB = <AB>{};
41+
>ab : AB
42+
>AB : AB
43+
><AB>{} : AB
44+
>AB : AB
45+
>{} : {}
46+
47+
if (ab.type === 'a') {
48+
>ab.type === 'a' : boolean
49+
>ab.type : "a" | "b"
50+
>ab : AB
51+
>type : "a" | "b"
52+
>'a' : "a"
53+
54+
if (ab.subtype === 2) {
55+
>ab.subtype === 2 : boolean
56+
>ab.subtype : 1 | 2
57+
>ab : A1 | A2
58+
>subtype : 1 | 2
59+
>2 : 2
60+
61+
ab.foo;
62+
>ab.foo : number
63+
>ab : A2
64+
>foo : number
65+
}
66+
}
67+
68+
// Repro from #11185
69+
70+
class Square { kind: "square"; }
71+
>Square : Square
72+
>kind : "square"
73+
74+
class Circle { kind: "circle"; }
75+
>Circle : Circle
76+
>kind : "circle"
77+
78+
type Shape = Circle | Square;
79+
>Shape : Shape
80+
>Circle : Circle
81+
>Square : Square
82+
83+
type Shapes = Shape | Array<Shape>;
84+
>Shapes : Shapes
85+
>Shape : Shape
86+
>Array : T[]
87+
>Shape : Shape
88+
89+
function isShape(s : Shapes): s is Shape {
90+
>isShape : (s: Shapes) => s is Shape
91+
>s : Shapes
92+
>Shapes : Shapes
93+
>s : any
94+
>Shape : Shape
95+
96+
return !Array.isArray(s);
97+
>!Array.isArray(s) : boolean
98+
>Array.isArray(s) : boolean
99+
>Array.isArray : (arg: any) => arg is any[]
100+
>Array : ArrayConstructor
101+
>isArray : (arg: any) => arg is any[]
102+
>s : Shapes
103+
}
104+
105+
function fail(s: Shapes) {
106+
>fail : (s: Shapes) => void
107+
>s : Shapes
108+
>Shapes : Shapes
109+
110+
if (isShape(s)) {
111+
>isShape(s) : boolean
112+
>isShape : (s: Shapes) => s is Shape
113+
>s : Shapes
114+
115+
if (s.kind === "circle") {
116+
>s.kind === "circle" : boolean
117+
>s.kind : "square" | "circle"
118+
>s : Shape
119+
>kind : "square" | "circle"
120+
>"circle" : "circle"
121+
122+
let c: Circle = s;
123+
>c : Circle
124+
>Circle : Circle
125+
>s : Circle
126+
}
127+
}
128+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Repro from #10586
2+
3+
interface A1 {
4+
type: 'a';
5+
subtype: 1;
6+
}
7+
8+
interface A2 {
9+
type: 'a';
10+
subtype: 2;
11+
foo: number;
12+
}
13+
14+
interface B {
15+
type: 'b';
16+
}
17+
18+
type AB = A1 | A2 | B;
19+
20+
const ab: AB = <AB>{};
21+
22+
if (ab.type === 'a') {
23+
if (ab.subtype === 2) {
24+
ab.foo;
25+
}
26+
}
27+
28+
// Repro from #11185
29+
30+
class Square { kind: "square"; }
31+
class Circle { kind: "circle"; }
32+
33+
type Shape = Circle | Square;
34+
type Shapes = Shape | Array<Shape>;
35+
36+
function isShape(s : Shapes): s is Shape {
37+
return !Array.isArray(s);
38+
}
39+
40+
function fail(s: Shapes) {
41+
if (isShape(s)) {
42+
if (s.kind === "circle") {
43+
let c: Circle = s;
44+
}
45+
}
46+
}

0 commit comments

Comments
 (0)