Skip to content

Better typings for Array.concat(), etc. #33645

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
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
1 change: 1 addition & 0 deletions src/harness/fourslashInterfaceImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -954,6 +954,7 @@ namespace FourSlashInterface {
varEntry("JSON"),
interfaceEntry("ReadonlyArray"),
interfaceEntry("ConcatArray"),
typeEntry("Flatten"),
varEntry("Array"),
interfaceEntry("ArrayConstructor"),
interfaceEntry("TypedPropertyDescriptor"),
Expand Down
182 changes: 20 additions & 162 deletions src/lib/es2019.array.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,106 +10,23 @@ interface ReadonlyArray<T> {
* @param thisArg An object to which the this keyword can refer in the callback function. If
* thisArg is omitted, undefined is used as the this value.
*/
flatMap<U, This = undefined> (
callback: (this: This, value: T, index: number, array: T[]) => U | ReadonlyArray<U>,
thisArg?: This
): U[]


/**
* Returns a new array with all sub-array elements concatenated into it recursively up to the
* specified depth.
*
* @param depth The maximum recursion depth
*/
flat<U>(this:
ReadonlyArray<U[][][][]> |

ReadonlyArray<ReadonlyArray<U[][][]>> |
ReadonlyArray<ReadonlyArray<U[][]>[]> |
ReadonlyArray<ReadonlyArray<U[]>[][]> |
ReadonlyArray<ReadonlyArray<U>[][][]> |

ReadonlyArray<ReadonlyArray<ReadonlyArray<U[][]>>> |
ReadonlyArray<ReadonlyArray<ReadonlyArray<U>[][]>> |
ReadonlyArray<ReadonlyArray<ReadonlyArray<U>>[][]> |
ReadonlyArray<ReadonlyArray<ReadonlyArray<U>[]>[]> |
ReadonlyArray<ReadonlyArray<ReadonlyArray<U[]>>[]> |
ReadonlyArray<ReadonlyArray<ReadonlyArray<U[]>[]>> |

ReadonlyArray<ReadonlyArray<ReadonlyArray<ReadonlyArray<U[]>>>> |
ReadonlyArray<ReadonlyArray<ReadonlyArray<ReadonlyArray<U>[]>>> |
ReadonlyArray<ReadonlyArray<ReadonlyArray<ReadonlyArray<U>>[]>> |
ReadonlyArray<ReadonlyArray<ReadonlyArray<ReadonlyArray<U>>>[]> |

ReadonlyArray<ReadonlyArray<ReadonlyArray<ReadonlyArray<ReadonlyArray<U>>>>>,
depth: 4): U[];

/**
* Returns a new array with all sub-array elements concatenated into it recursively up to the
* specified depth.
*
* @param depth The maximum recursion depth
*/
flat<U>(this:
ReadonlyArray<U[][][]> |

ReadonlyArray<ReadonlyArray<U>[][]> |
ReadonlyArray<ReadonlyArray<U[]>[]> |
ReadonlyArray<ReadonlyArray<U[][]>> |

ReadonlyArray<ReadonlyArray<ReadonlyArray<U[]>>> |
ReadonlyArray<ReadonlyArray<ReadonlyArray<U>[]>> |
ReadonlyArray<ReadonlyArray<ReadonlyArray<U>>[]> |

ReadonlyArray<ReadonlyArray<ReadonlyArray<ReadonlyArray<U>>>>,
depth: 3): U[];

/**
* Returns a new array with all sub-array elements concatenated into it recursively up to the
* specified depth.
*
* @param depth The maximum recursion depth
*/
flat<U>(this:
ReadonlyArray<U[][]> |

ReadonlyArray<ReadonlyArray<U[]>> |
ReadonlyArray<ReadonlyArray<U>[]> |

ReadonlyArray<ReadonlyArray<ReadonlyArray<U>>>,
depth: 2): U[];

/**
* Returns a new array with all sub-array elements concatenated into it recursively up to the
* specified depth.
*
* @param depth The maximum recursion depth
*/
flat<U>(this:
ReadonlyArray<U[]> |
ReadonlyArray<ReadonlyArray<U>>,
depth?: 1
): U[];
flatMap<U> (
callbackfn: (value: T, index: number, array: readonly T[]) => U,
thisArg?: any
): Flatten<U>[];

/**
* Returns a new array with all sub-array elements concatenated into it recursively up to the
* specified depth.
*
* @param depth The maximum recursion depth
*/
flat<U>(this:
ReadonlyArray<U>,
depth: 0
): U[];

/**
* Returns a new array with all sub-array elements concatenated into it recursively up to the
* specified depth. If no depth is provided, flat method defaults to the depth of 1.
*
* @param depth The maximum recursion depth
*/
flat<U>(depth?: number): any[];
flat(depth: 4): Flatten<Flatten<Flatten<Flatten<T>>>>[];
flat(depth: 3): Flatten<Flatten<Flatten<T>>>[];
flat(depth: 2): Flatten<Flatten<T>>[];
flat(depth?: 1): Flatten<T>[];
flat(depth: 0): T[];
flat(depth: number): Flatten<Flatten<Flatten<Flatten<T>>>> extends readonly any[] ? unknown[] : Flatten<Flatten<Flatten<Flatten<T>>>>[] | Flatten<Flatten<Flatten<T>>>[] | Flatten<Flatten<T>>[] | Flatten<T>[] | T[];
}

interface Array<T> {
Expand All @@ -124,80 +41,21 @@ interface Array<T> {
* @param thisArg An object to which the this keyword can refer in the callback function. If
* thisArg is omitted, undefined is used as the this value.
*/
flatMap<U, This = undefined> (
callback: (this: This, value: T, index: number, array: T[]) => U | ReadonlyArray<U>,
thisArg?: This
): U[]
flatMap<U> (
callbackfn: (value: T, index: number, array: T[]) => U,
thisArg?: any
): Flatten<U>[];

/**
* Returns a new array with all sub-array elements concatenated into it recursively up to the
* specified depth.
*
* @param depth The maximum recursion depth
*/
flat<U>(this: U[][][][][][][][], depth: 7): U[];

/**
* Returns a new array with all sub-array elements concatenated into it recursively up to the
* specified depth.
*
* @param depth The maximum recursion depth
*/
flat<U>(this: U[][][][][][][], depth: 6): U[];

/**
* Returns a new array with all sub-array elements concatenated into it recursively up to the
* specified depth.
*
* @param depth The maximum recursion depth
*/
flat<U>(this: U[][][][][][], depth: 5): U[];

/**
* Returns a new array with all sub-array elements concatenated into it recursively up to the
* specified depth.
*
* @param depth The maximum recursion depth
*/
flat<U>(this: U[][][][][], depth: 4): U[];

/**
* Returns a new array with all sub-array elements concatenated into it recursively up to the
* specified depth.
*
* @param depth The maximum recursion depth
*/
flat<U>(this: U[][][][], depth: 3): U[];

/**
* Returns a new array with all sub-array elements concatenated into it recursively up to the
* specified depth.
*
* @param depth The maximum recursion depth
*/
flat<U>(this: U[][][], depth: 2): U[];

/**
* Returns a new array with all sub-array elements concatenated into it recursively up to the
* specified depth.
*
* @param depth The maximum recursion depth
*/
flat<U>(this: U[][], depth?: 1): U[];

/**
* Returns a new array with all sub-array elements concatenated into it recursively up to the
* specified depth.
*
* @param depth The maximum recursion depth
*/
flat<U>(this: U[], depth: 0): U[];

/**
* Returns a new array with all sub-array elements concatenated into it recursively up to the
* specified depth. If no depth is provided, flat method defaults to the depth of 1.
*
* @param depth The maximum recursion depth
*/
flat<U>(depth?: number): any[];
flat(depth: 4): Flatten<Flatten<Flatten<Flatten<T>>>>[];
flat(depth: 3): Flatten<Flatten<Flatten<T>>>[];
flat(depth: 2): Flatten<Flatten<T>>[];
flat(depth?: 1): Flatten<T>[];
flat(depth: 0): T[];
flat(depth: number): Flatten<Flatten<Flatten<Flatten<T>>>> extends readonly any[] ? unknown[] : Flatten<Flatten<Flatten<Flatten<T>>>>[] | Flatten<Flatten<Flatten<T>>>[] | Flatten<Flatten<T>>[] | Flatten<T>[] | T[];
}
13 changes: 3 additions & 10 deletions src/lib/es5.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1081,11 +1081,7 @@ interface ReadonlyArray<T> {
* @param items Additional items to add to the end of array1.
*/
concat(...items: ConcatArray<T>[]): T[];
/**
* Combines two or more arrays.
* @param items Additional items to add to the end of array1.
*/
concat(...items: (T | ConcatArray<T>)[]): T[];
concat<U extends any[]>(...items: U): (T | Flatten<U[number]>)[];
/**
* Adds all the elements of an array separated by the specified separator string.
* @param separator A string used to separate one element of an array from the next in the resulting String. If omitted, the array elements are separated with a comma.
Expand Down Expand Up @@ -1187,6 +1183,7 @@ interface ConcatArray<T> {
join(separator?: string): string;
slice(start?: number, end?: number): T[];
}
type Flatten<T> = T extends undefined ? T : T extends ConcatArray<infer U> ? U : T;

interface Array<T> {
/**
Expand Down Expand Up @@ -1215,11 +1212,7 @@ interface Array<T> {
* @param items Additional items to add to the end of array1.
*/
concat(...items: ConcatArray<T>[]): T[];
/**
* Combines two or more arrays.
* @param items Additional items to add to the end of array1.
*/
concat(...items: (T | ConcatArray<T>)[]): T[];
concat<U extends any[]>(...items: U): (T | Flatten<U[number]>)[];
/**
* Adds all the elements of an array separated by the specified separator string.
* @param separator A string used to separate one element of an array from the next in the resulting String. If omitted, the array elements are separated with a comma.
Expand Down
29 changes: 29 additions & 0 deletions tests/baselines/reference/arrayConcat2.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,24 @@ a.concat('Hello');

var b = new Array<string>();
b.concat('hello');

const expected1: undefined = undefined as Flatten<undefined>;

// #19535

let [actual2] = (undefined as unknown as string[][]).concat([""]);
const expected2: string | string[] = actual2;
actual2 = undefined as unknown as string | string[];

// #26378

let [actual3] = [""].concat([1]);
const expected3: string | number = actual3;
actual3 = undefined as unknown as string | number;

// #26976

[].concat([""]);


//// [arrayConcat2.js]
Expand All @@ -14,3 +32,14 @@ a.concat("hello", 'world');
a.concat('Hello');
var b = new Array();
b.concat('hello');
var expected1 = undefined;
// #19535
var actual2 = undefined.concat([""])[0];
var expected2 = actual2;
actual2 = undefined;
// #26378
var actual3 = [""].concat([1])[0];
var expected3 = actual3;
actual3 = undefined;
// #26976
[].concat([""]);
42 changes: 42 additions & 0 deletions tests/baselines/reference/arrayConcat2.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,45 @@ b.concat('hello');
>b : Symbol(b, Decl(arrayConcat2.ts, 5, 3))
>concat : Symbol(Array.concat, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))

const expected1: undefined = undefined as Flatten<undefined>;
>expected1 : Symbol(expected1, Decl(arrayConcat2.ts, 8, 5))
>undefined : Symbol(undefined)
>Flatten : Symbol(Flatten, Decl(lib.es5.d.ts, --, --))

// #19535

let [actual2] = (undefined as unknown as string[][]).concat([""]);
>actual2 : Symbol(actual2, Decl(arrayConcat2.ts, 12, 5))
>(undefined as unknown as string[][]).concat : Symbol(Array.concat, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>undefined : Symbol(undefined)
>concat : Symbol(Array.concat, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))

const expected2: string | string[] = actual2;
>expected2 : Symbol(expected2, Decl(arrayConcat2.ts, 13, 5))
>actual2 : Symbol(actual2, Decl(arrayConcat2.ts, 12, 5))

actual2 = undefined as unknown as string | string[];
>actual2 : Symbol(actual2, Decl(arrayConcat2.ts, 12, 5))
>undefined : Symbol(undefined)

// #26378

let [actual3] = [""].concat([1]);
>actual3 : Symbol(actual3, Decl(arrayConcat2.ts, 18, 5))
>[""].concat : Symbol(Array.concat, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>concat : Symbol(Array.concat, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))

const expected3: string | number = actual3;
>expected3 : Symbol(expected3, Decl(arrayConcat2.ts, 19, 5))
>actual3 : Symbol(actual3, Decl(arrayConcat2.ts, 18, 5))

actual3 = undefined as unknown as string | number;
>actual3 : Symbol(actual3, Decl(arrayConcat2.ts, 18, 5))
>undefined : Symbol(undefined)

// #26976

[].concat([""]);
>[].concat : Symbol(Array.concat, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>concat : Symbol(Array.concat, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))

Loading