Skip to content

Commit

Permalink
Add tests and the new isArray definition
Browse files Browse the repository at this point in the history
  • Loading branch information
orta committed Jun 25, 2020
1 parent 636bc9d commit e81d845
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/lib/es5.d.ts
Expand Up @@ -1376,7 +1376,7 @@ interface ArrayConstructor {
(arrayLength?: number): any[];
<T>(arrayLength: number): T[];
<T>(...items: T[]): T[];
isArray(arg: any): arg is any[];
isArray<T>(arg: T | {}): arg is T extends readonly any[] ? readonly any[] : any[];
readonly prototype: any[];
}

Expand Down
28 changes: 15 additions & 13 deletions src/blank.ts → ...mpiler/consistentUnionSubtypeReduction.ts
@@ -1,3 +1,5 @@
// https://github.com/microsoft/TypeScript/issues/31155

declare const MyArray: {
isArray<T>(arg: T | {}): arg is T extends readonly any[] ? readonly any[] : any[];
};
Expand All @@ -7,23 +9,23 @@ declare const b: string[] | string;
declare const c: unknown;

if (MyArray.isArray(a)) {
a;
a; // readonly string[]
}
else {
a;
a; // string
}
a;
a; // readonly string[] | string;

if (MyArray.isArray(b)) {
b;
b; // string[] | string;
}
else {
b;
b; // string
}
b;
b; // string[] | string;

if (MyArray.isArray(c)) {
c;
c; // any[]
}


Expand All @@ -33,22 +35,22 @@ function f<T>(x: T) {
const c: T = null!;

if (MyArray.isArray(a)) {
a;
a; // readonly T[]
}
else {
a;
a; // string
}
a;
a; // readonly T[] | string;

if (MyArray.isArray(b)) {
b;
b; // T[]
}
else {
b;
b; // string
}
b;

if (MyArray.isArray(c)) {
c;
c; // T & (T extends readonly any[] ? readonly any[] : any[])
}
}

0 comments on commit e81d845

Please sign in to comment.