Skip to content

Commit 636bc9d

Browse files
committed
Explore using a different isArray declaration
1 parent 785e447 commit 636bc9d

File tree

2 files changed

+59
-9
lines changed

2 files changed

+59
-9
lines changed

src/blank.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
declare const MyArray: {
2+
isArray<T>(arg: T | {}): arg is T extends readonly any[] ? readonly any[] : any[];
3+
};
4+
5+
declare const a: readonly string[] | string;
6+
declare const b: string[] | string;
7+
declare const c: unknown;
8+
9+
if (MyArray.isArray(a)) {
10+
a;
11+
}
12+
else {
13+
a;
14+
}
15+
a;
16+
17+
if (MyArray.isArray(b)) {
18+
b;
19+
}
20+
else {
21+
b;
22+
}
23+
b;
24+
25+
if (MyArray.isArray(c)) {
26+
c;
27+
}
28+
29+
30+
function f<T>(x: T) {
31+
const a: readonly T[] | string = null!;
32+
const b: T[] | string = null!;
33+
const c: T = null!;
34+
35+
if (MyArray.isArray(a)) {
36+
a;
37+
}
38+
else {
39+
a;
40+
}
41+
a;
42+
43+
if (MyArray.isArray(b)) {
44+
b;
45+
}
46+
else {
47+
b;
48+
}
49+
b;
50+
51+
if (MyArray.isArray(c)) {
52+
c;
53+
}
54+
}

src/compiler/checker.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21761,15 +21761,11 @@ namespace ts {
2176121761
return assignableType;
2176221762
}
2176321763
}
21764-
// If the candidate type is a subtype of the target type, narrow to the candidate type.
21765-
// Otherwise, if the target type is assignable to the candidate type, keep the target type.
21766-
// Otherwise, if the candidate type is assignable to the target type, narrow to the candidate
21767-
// type. Otherwise, the types are completely unrelated, so narrow to an intersection of the
21768-
// two types.
21769-
return isTypeSubtypeOf(candidate, type) ? candidate :
21770-
isTypeAssignableTo(type, candidate) ? type :
21771-
isTypeAssignableTo(candidate, type) ? candidate :
21772-
getIntersectionType([type, candidate]);
21764+
21765+
// If the candidate type is a subtype of the target type, narrow to the candidate type,
21766+
// if the target type is a subtype of the candidate type, narrow to the target type,
21767+
// otherwise, narrow to an intersection of the two types.
21768+
return isTypeSubtypeOf(candidate, type) ? candidate : isTypeSubtypeOf(type, candidate) ? type : getIntersectionType([type, candidate]);
2177321769
}
2177421770

2177521771
function narrowTypeByCallExpression(type: Type, callExpression: CallExpression, assumeTrue: boolean): Type {

0 commit comments

Comments
 (0)