Skip to content

Commit 60e855e

Browse files
committed
Merge pull request #3492 from Microsoft/useTypePredicatesInLib
Define Array.isArray using a type predicate
2 parents 72783bc + 1a52038 commit 60e855e

File tree

7 files changed

+83
-9
lines changed

7 files changed

+83
-9
lines changed

src/lib/core.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1150,7 +1150,7 @@ interface ArrayConstructor {
11501150
(arrayLength?: number): any[];
11511151
<T>(arrayLength: number): T[];
11521152
<T>(...items: T[]): T[];
1153-
isArray(arg: any): boolean;
1153+
isArray(arg: any): arg is Array<any>;
11541154
prototype: Array<any>;
11551155
}
11561156

tests/baselines/reference/isArray.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//// [isArray.ts]
2+
var maybeArray: number | number[];
3+
4+
5+
if (Array.isArray(maybeArray)) {
6+
maybeArray.length; // OK
7+
}
8+
else {
9+
maybeArray.toFixed(); // OK
10+
}
11+
12+
//// [isArray.js]
13+
var maybeArray;
14+
if (Array.isArray(maybeArray)) {
15+
maybeArray.length; // OK
16+
}
17+
else {
18+
maybeArray.toFixed(); // OK
19+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
=== tests/cases/compiler/isArray.ts ===
2+
var maybeArray: number | number[];
3+
>maybeArray : Symbol(maybeArray, Decl(isArray.ts, 0, 3))
4+
5+
6+
if (Array.isArray(maybeArray)) {
7+
>Array.isArray : Symbol(ArrayConstructor.isArray, Decl(lib.d.ts, 1166, 28))
8+
>Array : Symbol(Array, Decl(lib.d.ts, 1000, 23), Decl(lib.d.ts, 1171, 11))
9+
>isArray : Symbol(ArrayConstructor.isArray, Decl(lib.d.ts, 1166, 28))
10+
>maybeArray : Symbol(maybeArray, Decl(isArray.ts, 0, 3))
11+
12+
maybeArray.length; // OK
13+
>maybeArray.length : Symbol(Array.length, Decl(lib.d.ts, 1007, 20))
14+
>maybeArray : Symbol(maybeArray, Decl(isArray.ts, 0, 3))
15+
>length : Symbol(Array.length, Decl(lib.d.ts, 1007, 20))
16+
}
17+
else {
18+
maybeArray.toFixed(); // OK
19+
>maybeArray.toFixed : Symbol(Number.toFixed, Decl(lib.d.ts, 463, 37))
20+
>maybeArray : Symbol(maybeArray, Decl(isArray.ts, 0, 3))
21+
>toFixed : Symbol(Number.toFixed, Decl(lib.d.ts, 463, 37))
22+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
=== tests/cases/compiler/isArray.ts ===
2+
var maybeArray: number | number[];
3+
>maybeArray : number | number[]
4+
5+
6+
if (Array.isArray(maybeArray)) {
7+
>Array.isArray(maybeArray) : boolean
8+
>Array.isArray : (arg: any) => boolean
9+
>Array : ArrayConstructor
10+
>isArray : (arg: any) => boolean
11+
>maybeArray : number | number[]
12+
13+
maybeArray.length; // OK
14+
>maybeArray.length : number
15+
>maybeArray : number[]
16+
>length : number
17+
}
18+
else {
19+
maybeArray.toFixed(); // OK
20+
>maybeArray.toFixed() : string
21+
>maybeArray.toFixed : (fractionDigits?: number) => string
22+
>maybeArray : number
23+
>toFixed : (fractionDigits?: number) => string
24+
}

tests/baselines/reference/library_ArraySlice.symbols

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,22 @@
22
// Array.prototype.slice can have zero, one, or two arguments
33
Array.prototype.slice();
44
>Array.prototype.slice : Symbol(Array.slice, Decl(lib.d.ts, 1048, 15))
5-
>Array.prototype : Symbol(ArrayConstructor.prototype, Decl(lib.d.ts, 1167, 31))
5+
>Array.prototype : Symbol(ArrayConstructor.prototype, Decl(lib.d.ts, 1167, 41))
66
>Array : Symbol(Array, Decl(lib.d.ts, 1000, 23), Decl(lib.d.ts, 1171, 11))
7-
>prototype : Symbol(ArrayConstructor.prototype, Decl(lib.d.ts, 1167, 31))
7+
>prototype : Symbol(ArrayConstructor.prototype, Decl(lib.d.ts, 1167, 41))
88
>slice : Symbol(Array.slice, Decl(lib.d.ts, 1048, 15))
99

1010
Array.prototype.slice(0);
1111
>Array.prototype.slice : Symbol(Array.slice, Decl(lib.d.ts, 1048, 15))
12-
>Array.prototype : Symbol(ArrayConstructor.prototype, Decl(lib.d.ts, 1167, 31))
12+
>Array.prototype : Symbol(ArrayConstructor.prototype, Decl(lib.d.ts, 1167, 41))
1313
>Array : Symbol(Array, Decl(lib.d.ts, 1000, 23), Decl(lib.d.ts, 1171, 11))
14-
>prototype : Symbol(ArrayConstructor.prototype, Decl(lib.d.ts, 1167, 31))
14+
>prototype : Symbol(ArrayConstructor.prototype, Decl(lib.d.ts, 1167, 41))
1515
>slice : Symbol(Array.slice, Decl(lib.d.ts, 1048, 15))
1616

1717
Array.prototype.slice(0, 1);
1818
>Array.prototype.slice : Symbol(Array.slice, Decl(lib.d.ts, 1048, 15))
19-
>Array.prototype : Symbol(ArrayConstructor.prototype, Decl(lib.d.ts, 1167, 31))
19+
>Array.prototype : Symbol(ArrayConstructor.prototype, Decl(lib.d.ts, 1167, 41))
2020
>Array : Symbol(Array, Decl(lib.d.ts, 1000, 23), Decl(lib.d.ts, 1171, 11))
21-
>prototype : Symbol(ArrayConstructor.prototype, Decl(lib.d.ts, 1167, 31))
21+
>prototype : Symbol(ArrayConstructor.prototype, Decl(lib.d.ts, 1167, 41))
2222
>slice : Symbol(Array.slice, Decl(lib.d.ts, 1048, 15))
2323

tests/baselines/reference/returnTypeParameterWithModules.symbols

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ module M1 {
1414
return Array.prototype.reduce.apply(ar, e ? [f, e] : [f]);
1515
>Array.prototype.reduce.apply : Symbol(Function.apply, Decl(lib.d.ts, 228, 20))
1616
>Array.prototype.reduce : Symbol(Array.reduce, Decl(lib.d.ts, 1129, 93), Decl(lib.d.ts, 1136, 120))
17-
>Array.prototype : Symbol(ArrayConstructor.prototype, Decl(lib.d.ts, 1167, 31))
17+
>Array.prototype : Symbol(ArrayConstructor.prototype, Decl(lib.d.ts, 1167, 41))
1818
>Array : Symbol(Array, Decl(lib.d.ts, 1000, 23), Decl(lib.d.ts, 1171, 11))
19-
>prototype : Symbol(ArrayConstructor.prototype, Decl(lib.d.ts, 1167, 31))
19+
>prototype : Symbol(ArrayConstructor.prototype, Decl(lib.d.ts, 1167, 41))
2020
>reduce : Symbol(Array.reduce, Decl(lib.d.ts, 1129, 93), Decl(lib.d.ts, 1136, 120))
2121
>apply : Symbol(Function.apply, Decl(lib.d.ts, 228, 20))
2222
>ar : Symbol(ar, Decl(returnTypeParameterWithModules.ts, 1, 30))

tests/cases/compiler/isArray.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
var maybeArray: number | number[];
2+
3+
4+
if (Array.isArray(maybeArray)) {
5+
maybeArray.length; // OK
6+
}
7+
else {
8+
maybeArray.toFixed(); // OK
9+
}

0 commit comments

Comments
 (0)