Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/lib/core.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1150,7 +1150,7 @@ interface ArrayConstructor {
(arrayLength?: number): any[];
<T>(arrayLength: number): T[];
<T>(...items: T[]): T[];
isArray(arg: any): boolean;
isArray(arg: any): arg is Array<any>;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we allow this to be generic? i.e.

isArray<T>(arg: any): arg is Array<T>

?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could, but we should add a separate overload for it. Otherwise, not passing the type argument gives you Array<{}>

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so are you expecting this to work:

var arr: Array<number> | Array<string>;

if (Array.isArray<number>(arr)) {
    arr[0].toFixed(); // but what does that mean at runtime?
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mhegazy I think you're right. Specifying the type is claiming to know something that you don't.

prototype: Array<any>;
}

Expand Down
19 changes: 19 additions & 0 deletions tests/baselines/reference/isArray.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//// [isArray.ts]
var maybeArray: number | number[];


if (Array.isArray(maybeArray)) {
maybeArray.length; // OK
}
else {
maybeArray.toFixed(); // OK
}

//// [isArray.js]
var maybeArray;
if (Array.isArray(maybeArray)) {
maybeArray.length; // OK
}
else {
maybeArray.toFixed(); // OK
}
22 changes: 22 additions & 0 deletions tests/baselines/reference/isArray.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
=== tests/cases/compiler/isArray.ts ===
var maybeArray: number | number[];
>maybeArray : Symbol(maybeArray, Decl(isArray.ts, 0, 3))


if (Array.isArray(maybeArray)) {
>Array.isArray : Symbol(ArrayConstructor.isArray, Decl(lib.d.ts, 1166, 28))
>Array : Symbol(Array, Decl(lib.d.ts, 1000, 23), Decl(lib.d.ts, 1171, 11))
>isArray : Symbol(ArrayConstructor.isArray, Decl(lib.d.ts, 1166, 28))
>maybeArray : Symbol(maybeArray, Decl(isArray.ts, 0, 3))

maybeArray.length; // OK
>maybeArray.length : Symbol(Array.length, Decl(lib.d.ts, 1007, 20))
>maybeArray : Symbol(maybeArray, Decl(isArray.ts, 0, 3))
>length : Symbol(Array.length, Decl(lib.d.ts, 1007, 20))
}
else {
maybeArray.toFixed(); // OK
>maybeArray.toFixed : Symbol(Number.toFixed, Decl(lib.d.ts, 463, 37))
>maybeArray : Symbol(maybeArray, Decl(isArray.ts, 0, 3))
>toFixed : Symbol(Number.toFixed, Decl(lib.d.ts, 463, 37))
}
24 changes: 24 additions & 0 deletions tests/baselines/reference/isArray.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
=== tests/cases/compiler/isArray.ts ===
var maybeArray: number | number[];
>maybeArray : number | number[]


if (Array.isArray(maybeArray)) {
>Array.isArray(maybeArray) : boolean
>Array.isArray : (arg: any) => boolean
>Array : ArrayConstructor
>isArray : (arg: any) => boolean
>maybeArray : number | number[]

maybeArray.length; // OK
>maybeArray.length : number
>maybeArray : number[]
>length : number
}
else {
maybeArray.toFixed(); // OK
>maybeArray.toFixed() : string
>maybeArray.toFixed : (fractionDigits?: number) => string
>maybeArray : number
>toFixed : (fractionDigits?: number) => string
}
12 changes: 6 additions & 6 deletions tests/baselines/reference/library_ArraySlice.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@
// Array.prototype.slice can have zero, one, or two arguments
Array.prototype.slice();
>Array.prototype.slice : Symbol(Array.slice, Decl(lib.d.ts, 1048, 15))
>Array.prototype : Symbol(ArrayConstructor.prototype, Decl(lib.d.ts, 1167, 31))
>Array.prototype : Symbol(ArrayConstructor.prototype, Decl(lib.d.ts, 1167, 41))
>Array : Symbol(Array, Decl(lib.d.ts, 1000, 23), Decl(lib.d.ts, 1171, 11))
>prototype : Symbol(ArrayConstructor.prototype, Decl(lib.d.ts, 1167, 31))
>prototype : Symbol(ArrayConstructor.prototype, Decl(lib.d.ts, 1167, 41))
>slice : Symbol(Array.slice, Decl(lib.d.ts, 1048, 15))

Array.prototype.slice(0);
>Array.prototype.slice : Symbol(Array.slice, Decl(lib.d.ts, 1048, 15))
>Array.prototype : Symbol(ArrayConstructor.prototype, Decl(lib.d.ts, 1167, 31))
>Array.prototype : Symbol(ArrayConstructor.prototype, Decl(lib.d.ts, 1167, 41))
>Array : Symbol(Array, Decl(lib.d.ts, 1000, 23), Decl(lib.d.ts, 1171, 11))
>prototype : Symbol(ArrayConstructor.prototype, Decl(lib.d.ts, 1167, 31))
>prototype : Symbol(ArrayConstructor.prototype, Decl(lib.d.ts, 1167, 41))
>slice : Symbol(Array.slice, Decl(lib.d.ts, 1048, 15))

Array.prototype.slice(0, 1);
>Array.prototype.slice : Symbol(Array.slice, Decl(lib.d.ts, 1048, 15))
>Array.prototype : Symbol(ArrayConstructor.prototype, Decl(lib.d.ts, 1167, 31))
>Array.prototype : Symbol(ArrayConstructor.prototype, Decl(lib.d.ts, 1167, 41))
>Array : Symbol(Array, Decl(lib.d.ts, 1000, 23), Decl(lib.d.ts, 1171, 11))
>prototype : Symbol(ArrayConstructor.prototype, Decl(lib.d.ts, 1167, 31))
>prototype : Symbol(ArrayConstructor.prototype, Decl(lib.d.ts, 1167, 41))
>slice : Symbol(Array.slice, Decl(lib.d.ts, 1048, 15))

Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ module M1 {
return Array.prototype.reduce.apply(ar, e ? [f, e] : [f]);
>Array.prototype.reduce.apply : Symbol(Function.apply, Decl(lib.d.ts, 228, 20))
>Array.prototype.reduce : Symbol(Array.reduce, Decl(lib.d.ts, 1129, 93), Decl(lib.d.ts, 1136, 120))
>Array.prototype : Symbol(ArrayConstructor.prototype, Decl(lib.d.ts, 1167, 31))
>Array.prototype : Symbol(ArrayConstructor.prototype, Decl(lib.d.ts, 1167, 41))
>Array : Symbol(Array, Decl(lib.d.ts, 1000, 23), Decl(lib.d.ts, 1171, 11))
>prototype : Symbol(ArrayConstructor.prototype, Decl(lib.d.ts, 1167, 31))
>prototype : Symbol(ArrayConstructor.prototype, Decl(lib.d.ts, 1167, 41))
>reduce : Symbol(Array.reduce, Decl(lib.d.ts, 1129, 93), Decl(lib.d.ts, 1136, 120))
>apply : Symbol(Function.apply, Decl(lib.d.ts, 228, 20))
>ar : Symbol(ar, Decl(returnTypeParameterWithModules.ts, 1, 30))
Expand Down
9 changes: 9 additions & 0 deletions tests/cases/compiler/isArray.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
var maybeArray: number | number[];


if (Array.isArray(maybeArray)) {
maybeArray.length; // OK
}
else {
maybeArray.toFixed(); // OK
}