Skip to content

Commit d9b6723

Browse files
committed
Add regression test
1 parent 3e3b808 commit d9b6723

File tree

4 files changed

+425
-0
lines changed

4 files changed

+425
-0
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
//// [narrowingByDiscriminantInLoop.ts]
2+
// Repro from #9977
3+
4+
type IDLMemberTypes = OperationMemberType | ConstantMemberType;
5+
6+
interface IDLTypeDescription {
7+
origin: string;
8+
}
9+
10+
interface InterfaceType {
11+
members: IDLMemberTypes[];
12+
}
13+
14+
interface OperationMemberType {
15+
type: "operation";
16+
idlType: IDLTypeDescription | null;
17+
}
18+
19+
interface ConstantMemberType {
20+
type: "const";
21+
idlType: string;
22+
}
23+
24+
function insertInterface(callbackType: InterfaceType) {
25+
for (const memberType of callbackType.members) {
26+
if (memberType.type === "const") {
27+
memberType.idlType; // string
28+
}
29+
else if (memberType.type === "operation") {
30+
memberType.idlType.origin; // string
31+
(memberType.idlType as IDLTypeDescription);
32+
}
33+
}
34+
}
35+
36+
function insertInterface2(callbackType: InterfaceType) {
37+
for (const memberType of callbackType.members) {
38+
if (memberType.type === "operation") {
39+
memberType.idlType.origin; // string
40+
}
41+
}
42+
}
43+
44+
function foo(memberType: IDLMemberTypes) {
45+
if (memberType.type === "const") {
46+
memberType.idlType; // string
47+
}
48+
else if (memberType.type === "operation") {
49+
memberType.idlType.origin; // string
50+
}
51+
}
52+
53+
//// [narrowingByDiscriminantInLoop.js]
54+
// Repro from #9977
55+
function insertInterface(callbackType) {
56+
for (var _i = 0, _a = callbackType.members; _i < _a.length; _i++) {
57+
var memberType = _a[_i];
58+
if (memberType.type === "const") {
59+
memberType.idlType; // string
60+
}
61+
else if (memberType.type === "operation") {
62+
memberType.idlType.origin; // string
63+
memberType.idlType;
64+
}
65+
}
66+
}
67+
function insertInterface2(callbackType) {
68+
for (var _i = 0, _a = callbackType.members; _i < _a.length; _i++) {
69+
var memberType = _a[_i];
70+
if (memberType.type === "operation") {
71+
memberType.idlType.origin; // string
72+
}
73+
}
74+
}
75+
function foo(memberType) {
76+
if (memberType.type === "const") {
77+
memberType.idlType; // string
78+
}
79+
else if (memberType.type === "operation") {
80+
memberType.idlType.origin; // string
81+
}
82+
}
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
=== tests/cases/compiler/narrowingByDiscriminantInLoop.ts ===
2+
// Repro from #9977
3+
4+
type IDLMemberTypes = OperationMemberType | ConstantMemberType;
5+
>IDLMemberTypes : Symbol(IDLMemberTypes, Decl(narrowingByDiscriminantInLoop.ts, 0, 0))
6+
>OperationMemberType : Symbol(OperationMemberType, Decl(narrowingByDiscriminantInLoop.ts, 10, 1))
7+
>ConstantMemberType : Symbol(ConstantMemberType, Decl(narrowingByDiscriminantInLoop.ts, 15, 1))
8+
9+
interface IDLTypeDescription {
10+
>IDLTypeDescription : Symbol(IDLTypeDescription, Decl(narrowingByDiscriminantInLoop.ts, 2, 63))
11+
12+
origin: string;
13+
>origin : Symbol(IDLTypeDescription.origin, Decl(narrowingByDiscriminantInLoop.ts, 4, 30))
14+
}
15+
16+
interface InterfaceType {
17+
>InterfaceType : Symbol(InterfaceType, Decl(narrowingByDiscriminantInLoop.ts, 6, 1))
18+
19+
members: IDLMemberTypes[];
20+
>members : Symbol(InterfaceType.members, Decl(narrowingByDiscriminantInLoop.ts, 8, 25))
21+
>IDLMemberTypes : Symbol(IDLMemberTypes, Decl(narrowingByDiscriminantInLoop.ts, 0, 0))
22+
}
23+
24+
interface OperationMemberType {
25+
>OperationMemberType : Symbol(OperationMemberType, Decl(narrowingByDiscriminantInLoop.ts, 10, 1))
26+
27+
type: "operation";
28+
>type : Symbol(OperationMemberType.type, Decl(narrowingByDiscriminantInLoop.ts, 12, 31))
29+
30+
idlType: IDLTypeDescription | null;
31+
>idlType : Symbol(OperationMemberType.idlType, Decl(narrowingByDiscriminantInLoop.ts, 13, 22))
32+
>IDLTypeDescription : Symbol(IDLTypeDescription, Decl(narrowingByDiscriminantInLoop.ts, 2, 63))
33+
}
34+
35+
interface ConstantMemberType {
36+
>ConstantMemberType : Symbol(ConstantMemberType, Decl(narrowingByDiscriminantInLoop.ts, 15, 1))
37+
38+
type: "const";
39+
>type : Symbol(ConstantMemberType.type, Decl(narrowingByDiscriminantInLoop.ts, 17, 30))
40+
41+
idlType: string;
42+
>idlType : Symbol(ConstantMemberType.idlType, Decl(narrowingByDiscriminantInLoop.ts, 18, 18))
43+
}
44+
45+
function insertInterface(callbackType: InterfaceType) {
46+
>insertInterface : Symbol(insertInterface, Decl(narrowingByDiscriminantInLoop.ts, 20, 1))
47+
>callbackType : Symbol(callbackType, Decl(narrowingByDiscriminantInLoop.ts, 22, 25))
48+
>InterfaceType : Symbol(InterfaceType, Decl(narrowingByDiscriminantInLoop.ts, 6, 1))
49+
50+
for (const memberType of callbackType.members) {
51+
>memberType : Symbol(memberType, Decl(narrowingByDiscriminantInLoop.ts, 23, 14))
52+
>callbackType.members : Symbol(InterfaceType.members, Decl(narrowingByDiscriminantInLoop.ts, 8, 25))
53+
>callbackType : Symbol(callbackType, Decl(narrowingByDiscriminantInLoop.ts, 22, 25))
54+
>members : Symbol(InterfaceType.members, Decl(narrowingByDiscriminantInLoop.ts, 8, 25))
55+
56+
if (memberType.type === "const") {
57+
>memberType.type : Symbol(type, Decl(narrowingByDiscriminantInLoop.ts, 12, 31), Decl(narrowingByDiscriminantInLoop.ts, 17, 30))
58+
>memberType : Symbol(memberType, Decl(narrowingByDiscriminantInLoop.ts, 23, 14))
59+
>type : Symbol(type, Decl(narrowingByDiscriminantInLoop.ts, 12, 31), Decl(narrowingByDiscriminantInLoop.ts, 17, 30))
60+
61+
memberType.idlType; // string
62+
>memberType.idlType : Symbol(ConstantMemberType.idlType, Decl(narrowingByDiscriminantInLoop.ts, 18, 18))
63+
>memberType : Symbol(memberType, Decl(narrowingByDiscriminantInLoop.ts, 23, 14))
64+
>idlType : Symbol(ConstantMemberType.idlType, Decl(narrowingByDiscriminantInLoop.ts, 18, 18))
65+
}
66+
else if (memberType.type === "operation") {
67+
>memberType.type : Symbol(OperationMemberType.type, Decl(narrowingByDiscriminantInLoop.ts, 12, 31))
68+
>memberType : Symbol(memberType, Decl(narrowingByDiscriminantInLoop.ts, 23, 14))
69+
>type : Symbol(OperationMemberType.type, Decl(narrowingByDiscriminantInLoop.ts, 12, 31))
70+
71+
memberType.idlType.origin; // string
72+
>memberType.idlType.origin : Symbol(IDLTypeDescription.origin, Decl(narrowingByDiscriminantInLoop.ts, 4, 30))
73+
>memberType.idlType : Symbol(OperationMemberType.idlType, Decl(narrowingByDiscriminantInLoop.ts, 13, 22))
74+
>memberType : Symbol(memberType, Decl(narrowingByDiscriminantInLoop.ts, 23, 14))
75+
>idlType : Symbol(OperationMemberType.idlType, Decl(narrowingByDiscriminantInLoop.ts, 13, 22))
76+
>origin : Symbol(IDLTypeDescription.origin, Decl(narrowingByDiscriminantInLoop.ts, 4, 30))
77+
78+
(memberType.idlType as IDLTypeDescription);
79+
>memberType.idlType : Symbol(OperationMemberType.idlType, Decl(narrowingByDiscriminantInLoop.ts, 13, 22))
80+
>memberType : Symbol(memberType, Decl(narrowingByDiscriminantInLoop.ts, 23, 14))
81+
>idlType : Symbol(OperationMemberType.idlType, Decl(narrowingByDiscriminantInLoop.ts, 13, 22))
82+
>IDLTypeDescription : Symbol(IDLTypeDescription, Decl(narrowingByDiscriminantInLoop.ts, 2, 63))
83+
}
84+
}
85+
}
86+
87+
function insertInterface2(callbackType: InterfaceType) {
88+
>insertInterface2 : Symbol(insertInterface2, Decl(narrowingByDiscriminantInLoop.ts, 32, 1))
89+
>callbackType : Symbol(callbackType, Decl(narrowingByDiscriminantInLoop.ts, 34, 26))
90+
>InterfaceType : Symbol(InterfaceType, Decl(narrowingByDiscriminantInLoop.ts, 6, 1))
91+
92+
for (const memberType of callbackType.members) {
93+
>memberType : Symbol(memberType, Decl(narrowingByDiscriminantInLoop.ts, 35, 14))
94+
>callbackType.members : Symbol(InterfaceType.members, Decl(narrowingByDiscriminantInLoop.ts, 8, 25))
95+
>callbackType : Symbol(callbackType, Decl(narrowingByDiscriminantInLoop.ts, 34, 26))
96+
>members : Symbol(InterfaceType.members, Decl(narrowingByDiscriminantInLoop.ts, 8, 25))
97+
98+
if (memberType.type === "operation") {
99+
>memberType.type : Symbol(type, Decl(narrowingByDiscriminantInLoop.ts, 12, 31), Decl(narrowingByDiscriminantInLoop.ts, 17, 30))
100+
>memberType : Symbol(memberType, Decl(narrowingByDiscriminantInLoop.ts, 35, 14))
101+
>type : Symbol(type, Decl(narrowingByDiscriminantInLoop.ts, 12, 31), Decl(narrowingByDiscriminantInLoop.ts, 17, 30))
102+
103+
memberType.idlType.origin; // string
104+
>memberType.idlType.origin : Symbol(IDLTypeDescription.origin, Decl(narrowingByDiscriminantInLoop.ts, 4, 30))
105+
>memberType.idlType : Symbol(OperationMemberType.idlType, Decl(narrowingByDiscriminantInLoop.ts, 13, 22))
106+
>memberType : Symbol(memberType, Decl(narrowingByDiscriminantInLoop.ts, 35, 14))
107+
>idlType : Symbol(OperationMemberType.idlType, Decl(narrowingByDiscriminantInLoop.ts, 13, 22))
108+
>origin : Symbol(IDLTypeDescription.origin, Decl(narrowingByDiscriminantInLoop.ts, 4, 30))
109+
}
110+
}
111+
}
112+
113+
function foo(memberType: IDLMemberTypes) {
114+
>foo : Symbol(foo, Decl(narrowingByDiscriminantInLoop.ts, 40, 1))
115+
>memberType : Symbol(memberType, Decl(narrowingByDiscriminantInLoop.ts, 42, 13))
116+
>IDLMemberTypes : Symbol(IDLMemberTypes, Decl(narrowingByDiscriminantInLoop.ts, 0, 0))
117+
118+
if (memberType.type === "const") {
119+
>memberType.type : Symbol(type, Decl(narrowingByDiscriminantInLoop.ts, 12, 31), Decl(narrowingByDiscriminantInLoop.ts, 17, 30))
120+
>memberType : Symbol(memberType, Decl(narrowingByDiscriminantInLoop.ts, 42, 13))
121+
>type : Symbol(type, Decl(narrowingByDiscriminantInLoop.ts, 12, 31), Decl(narrowingByDiscriminantInLoop.ts, 17, 30))
122+
123+
memberType.idlType; // string
124+
>memberType.idlType : Symbol(ConstantMemberType.idlType, Decl(narrowingByDiscriminantInLoop.ts, 18, 18))
125+
>memberType : Symbol(memberType, Decl(narrowingByDiscriminantInLoop.ts, 42, 13))
126+
>idlType : Symbol(ConstantMemberType.idlType, Decl(narrowingByDiscriminantInLoop.ts, 18, 18))
127+
}
128+
else if (memberType.type === "operation") {
129+
>memberType.type : Symbol(OperationMemberType.type, Decl(narrowingByDiscriminantInLoop.ts, 12, 31))
130+
>memberType : Symbol(memberType, Decl(narrowingByDiscriminantInLoop.ts, 42, 13))
131+
>type : Symbol(OperationMemberType.type, Decl(narrowingByDiscriminantInLoop.ts, 12, 31))
132+
133+
memberType.idlType.origin; // string
134+
>memberType.idlType.origin : Symbol(IDLTypeDescription.origin, Decl(narrowingByDiscriminantInLoop.ts, 4, 30))
135+
>memberType.idlType : Symbol(OperationMemberType.idlType, Decl(narrowingByDiscriminantInLoop.ts, 13, 22))
136+
>memberType : Symbol(memberType, Decl(narrowingByDiscriminantInLoop.ts, 42, 13))
137+
>idlType : Symbol(OperationMemberType.idlType, Decl(narrowingByDiscriminantInLoop.ts, 13, 22))
138+
>origin : Symbol(IDLTypeDescription.origin, Decl(narrowingByDiscriminantInLoop.ts, 4, 30))
139+
}
140+
}
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
=== tests/cases/compiler/narrowingByDiscriminantInLoop.ts ===
2+
// Repro from #9977
3+
4+
type IDLMemberTypes = OperationMemberType | ConstantMemberType;
5+
>IDLMemberTypes : IDLMemberTypes
6+
>OperationMemberType : OperationMemberType
7+
>ConstantMemberType : ConstantMemberType
8+
9+
interface IDLTypeDescription {
10+
>IDLTypeDescription : IDLTypeDescription
11+
12+
origin: string;
13+
>origin : string
14+
}
15+
16+
interface InterfaceType {
17+
>InterfaceType : InterfaceType
18+
19+
members: IDLMemberTypes[];
20+
>members : IDLMemberTypes[]
21+
>IDLMemberTypes : IDLMemberTypes
22+
}
23+
24+
interface OperationMemberType {
25+
>OperationMemberType : OperationMemberType
26+
27+
type: "operation";
28+
>type : "operation"
29+
30+
idlType: IDLTypeDescription | null;
31+
>idlType : IDLTypeDescription
32+
>IDLTypeDescription : IDLTypeDescription
33+
>null : null
34+
}
35+
36+
interface ConstantMemberType {
37+
>ConstantMemberType : ConstantMemberType
38+
39+
type: "const";
40+
>type : "const"
41+
42+
idlType: string;
43+
>idlType : string
44+
}
45+
46+
function insertInterface(callbackType: InterfaceType) {
47+
>insertInterface : (callbackType: InterfaceType) => void
48+
>callbackType : InterfaceType
49+
>InterfaceType : InterfaceType
50+
51+
for (const memberType of callbackType.members) {
52+
>memberType : IDLMemberTypes
53+
>callbackType.members : IDLMemberTypes[]
54+
>callbackType : InterfaceType
55+
>members : IDLMemberTypes[]
56+
57+
if (memberType.type === "const") {
58+
>memberType.type === "const" : boolean
59+
>memberType.type : "operation" | "const"
60+
>memberType : IDLMemberTypes
61+
>type : "operation" | "const"
62+
>"const" : "const"
63+
64+
memberType.idlType; // string
65+
>memberType.idlType : string
66+
>memberType : ConstantMemberType
67+
>idlType : string
68+
}
69+
else if (memberType.type === "operation") {
70+
>memberType.type === "operation" : boolean
71+
>memberType.type : "operation"
72+
>memberType : OperationMemberType
73+
>type : "operation"
74+
>"operation" : "operation"
75+
76+
memberType.idlType.origin; // string
77+
>memberType.idlType.origin : string
78+
>memberType.idlType : IDLTypeDescription
79+
>memberType : OperationMemberType
80+
>idlType : IDLTypeDescription
81+
>origin : string
82+
83+
(memberType.idlType as IDLTypeDescription);
84+
>(memberType.idlType as IDLTypeDescription) : IDLTypeDescription
85+
>memberType.idlType as IDLTypeDescription : IDLTypeDescription
86+
>memberType.idlType : IDLTypeDescription
87+
>memberType : OperationMemberType
88+
>idlType : IDLTypeDescription
89+
>IDLTypeDescription : IDLTypeDescription
90+
}
91+
}
92+
}
93+
94+
function insertInterface2(callbackType: InterfaceType) {
95+
>insertInterface2 : (callbackType: InterfaceType) => void
96+
>callbackType : InterfaceType
97+
>InterfaceType : InterfaceType
98+
99+
for (const memberType of callbackType.members) {
100+
>memberType : IDLMemberTypes
101+
>callbackType.members : IDLMemberTypes[]
102+
>callbackType : InterfaceType
103+
>members : IDLMemberTypes[]
104+
105+
if (memberType.type === "operation") {
106+
>memberType.type === "operation" : boolean
107+
>memberType.type : "operation" | "const"
108+
>memberType : IDLMemberTypes
109+
>type : "operation" | "const"
110+
>"operation" : "operation"
111+
112+
memberType.idlType.origin; // string
113+
>memberType.idlType.origin : string
114+
>memberType.idlType : IDLTypeDescription
115+
>memberType : OperationMemberType
116+
>idlType : IDLTypeDescription
117+
>origin : string
118+
}
119+
}
120+
}
121+
122+
function foo(memberType: IDLMemberTypes) {
123+
>foo : (memberType: IDLMemberTypes) => void
124+
>memberType : IDLMemberTypes
125+
>IDLMemberTypes : IDLMemberTypes
126+
127+
if (memberType.type === "const") {
128+
>memberType.type === "const" : boolean
129+
>memberType.type : "operation" | "const"
130+
>memberType : IDLMemberTypes
131+
>type : "operation" | "const"
132+
>"const" : "const"
133+
134+
memberType.idlType; // string
135+
>memberType.idlType : string
136+
>memberType : ConstantMemberType
137+
>idlType : string
138+
}
139+
else if (memberType.type === "operation") {
140+
>memberType.type === "operation" : boolean
141+
>memberType.type : "operation"
142+
>memberType : OperationMemberType
143+
>type : "operation"
144+
>"operation" : "operation"
145+
146+
memberType.idlType.origin; // string
147+
>memberType.idlType.origin : string
148+
>memberType.idlType : IDLTypeDescription
149+
>memberType : OperationMemberType
150+
>idlType : IDLTypeDescription
151+
>origin : string
152+
}
153+
}

0 commit comments

Comments
 (0)