From 1a520386bb50454e2fae76a5a80f6a7799c0553c Mon Sep 17 00:00:00 2001 From: Mohamed Hegazy Date: Fri, 12 Jun 2015 09:32:15 -0700 Subject: [PATCH] Define Array.isArray using a type predicate --- src/lib/core.d.ts | 2 +- tests/baselines/reference/isArray.js | 19 +++++++++++++++ tests/baselines/reference/isArray.symbols | 22 +++++++++++++++++ tests/baselines/reference/isArray.types | 24 +++++++++++++++++++ .../reference/library_ArraySlice.symbols | 12 +++++----- .../returnTypeParameterWithModules.symbols | 4 ++-- tests/cases/compiler/isArray.ts | 9 +++++++ 7 files changed, 83 insertions(+), 9 deletions(-) create mode 100644 tests/baselines/reference/isArray.js create mode 100644 tests/baselines/reference/isArray.symbols create mode 100644 tests/baselines/reference/isArray.types create mode 100644 tests/cases/compiler/isArray.ts diff --git a/src/lib/core.d.ts b/src/lib/core.d.ts index c03ab344ab..7897196566 100644 --- a/src/lib/core.d.ts +++ b/src/lib/core.d.ts @@ -1150,7 +1150,7 @@ interface ArrayConstructor { (arrayLength?: number): any[]; (arrayLength: number): T[]; (...items: T[]): T[]; - isArray(arg: any): boolean; + isArray(arg: any): arg is Array; prototype: Array; } diff --git a/tests/baselines/reference/isArray.js b/tests/baselines/reference/isArray.js new file mode 100644 index 0000000000..a90898fd65 --- /dev/null +++ b/tests/baselines/reference/isArray.js @@ -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 +} diff --git a/tests/baselines/reference/isArray.symbols b/tests/baselines/reference/isArray.symbols new file mode 100644 index 0000000000..75351b377b --- /dev/null +++ b/tests/baselines/reference/isArray.symbols @@ -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)) +} diff --git a/tests/baselines/reference/isArray.types b/tests/baselines/reference/isArray.types new file mode 100644 index 0000000000..8865ff73bc --- /dev/null +++ b/tests/baselines/reference/isArray.types @@ -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 +} diff --git a/tests/baselines/reference/library_ArraySlice.symbols b/tests/baselines/reference/library_ArraySlice.symbols index 25e6fbd5b5..02c58859d4 100644 --- a/tests/baselines/reference/library_ArraySlice.symbols +++ b/tests/baselines/reference/library_ArraySlice.symbols @@ -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)) diff --git a/tests/baselines/reference/returnTypeParameterWithModules.symbols b/tests/baselines/reference/returnTypeParameterWithModules.symbols index a064716bd8..c4b354ad92 100644 --- a/tests/baselines/reference/returnTypeParameterWithModules.symbols +++ b/tests/baselines/reference/returnTypeParameterWithModules.symbols @@ -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)) diff --git a/tests/cases/compiler/isArray.ts b/tests/cases/compiler/isArray.ts new file mode 100644 index 0000000000..dbdd875e21 --- /dev/null +++ b/tests/cases/compiler/isArray.ts @@ -0,0 +1,9 @@ +var maybeArray: number | number[]; + + +if (Array.isArray(maybeArray)) { + maybeArray.length; // OK +} +else { + maybeArray.toFixed(); // OK +} \ No newline at end of file