Skip to content

Commit 54703cd

Browse files
Add more test cases.
1 parent ae63c73 commit 54703cd

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

tests/cases/compiler/narrowingMutualSubtypes.ts

+38
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,41 @@ function example(x: Union) {
103103
if (is(x)) {}
104104
x; // Union
105105
}
106+
107+
function checksArrayOrObject1(obj: Record<string, any> | Record<string, any>[]) {
108+
// "accidentally" guards the first branch on the length
109+
if (Array.isArray(obj) && obj.length) {
110+
for (let key in obj) {
111+
if (obj[key] !== undefined) {
112+
console.log(obj[key])
113+
}
114+
}
115+
}
116+
else {
117+
// 'obj' should probably not include an array type here.
118+
for (let key in obj) {
119+
if (obj[key] !== undefined) {
120+
console.log(obj[key])
121+
}
122+
}
123+
}
124+
}
125+
126+
function checksArrayOrObject2(obj: Record<string, any> | Record<string, any>[]) {
127+
if (Array.isArray(obj)) {
128+
// obj should only be an array type here
129+
for (let key in obj) {
130+
if (obj[key] !== undefined) {
131+
console.log(obj[key])
132+
}
133+
}
134+
}
135+
else {
136+
// 'obj' should probably not include an array type here.
137+
for (let key in obj) {
138+
if (obj[key] !== undefined) {
139+
console.log(obj[key])
140+
}
141+
}
142+
}
143+
}

0 commit comments

Comments
 (0)