Skip to content

Commit 82959b2

Browse files
committed
Fix unsound Array.flatMap() typings
1 parent 54aab85 commit 82959b2

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

src/lib/es2019.array.d.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@ interface ReadonlyArray<T> {
1010
* @param thisArg An object to which the this keyword can refer in the callback function. If
1111
* thisArg is omitted, undefined is used as the this value.
1212
*/
13-
flatMap<U, This = undefined> (
14-
callback: (this: This, value: T, index: number, array: T[]) => U | ReadonlyArray<U>,
13+
flatMap<U, This = undefined, That extends this> (
14+
this: readonly T[],
15+
callback: (this: This, value: That[number], index: number, array: That) => U,
1516
thisArg?: This
16-
): U[]
17+
): (U extends readonly (infer V)[] ? V : U)[];
1718

1819

1920
/**
@@ -124,10 +125,11 @@ interface Array<T> {
124125
* @param thisArg An object to which the this keyword can refer in the callback function. If
125126
* thisArg is omitted, undefined is used as the this value.
126127
*/
127-
flatMap<U, This = undefined> (
128-
callback: (this: This, value: T, index: number, array: T[]) => U | ReadonlyArray<U>,
128+
flatMap<U, This = undefined, That extends this> (
129+
this: readonly T[],
130+
callback: (this: This, value: That[number], index: number, array: That) => U,
129131
thisArg?: This
130-
): U[]
132+
): (U extends readonly (infer V)[] ? V : U)[];
131133

132134
/**
133135
* Returns a new array with all sub-array elements concatenated into it recursively up to the

tests/baselines/reference/arrayFlatMap.types

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,17 @@ const readonlyArray: ReadonlyArray<number> = [];
99

1010
array.flatMap((): ReadonlyArray<number> => []); // ok
1111
>array.flatMap((): ReadonlyArray<number> => []) : number[]
12-
>array.flatMap : <U, This = undefined>(callback: (this: This, value: number, index: number, array: number[]) => U | readonly U[], thisArg?: This) => U[]
12+
>array.flatMap : <U, This = undefined, That extends number[]>(this: readonly number[], callback: (this: This, value: That[number], index: number, array: That) => U, thisArg?: This) => (U extends readonly (infer V)[] ? V : U)[]
1313
>array : number[]
14-
>flatMap : <U, This = undefined>(callback: (this: This, value: number, index: number, array: number[]) => U | readonly U[], thisArg?: This) => U[]
14+
>flatMap : <U, This = undefined, That extends number[]>(this: readonly number[], callback: (this: This, value: That[number], index: number, array: That) => U, thisArg?: This) => (U extends readonly (infer V)[] ? V : U)[]
1515
>(): ReadonlyArray<number> => [] : () => readonly number[]
1616
>[] : undefined[]
1717

1818
readonlyArray.flatMap((): ReadonlyArray<number> => []); // ok
1919
>readonlyArray.flatMap((): ReadonlyArray<number> => []) : number[]
20-
>readonlyArray.flatMap : <U, This = undefined>(callback: (this: This, value: number, index: number, array: number[]) => U | readonly U[], thisArg?: This) => U[]
20+
>readonlyArray.flatMap : <U, This = undefined, That extends readonly number[]>(this: readonly number[], callback: (this: This, value: That[number], index: number, array: That) => U, thisArg?: This) => (U extends readonly (infer V)[] ? V : U)[]
2121
>readonlyArray : readonly number[]
22-
>flatMap : <U, This = undefined>(callback: (this: This, value: number, index: number, array: number[]) => U | readonly U[], thisArg?: This) => U[]
22+
>flatMap : <U, This = undefined, That extends readonly number[]>(this: readonly number[], callback: (this: This, value: That[number], index: number, array: That) => U, thisArg?: This) => (U extends readonly (infer V)[] ? V : U)[]
2323
>(): ReadonlyArray<number> => [] : () => readonly number[]
2424
>[] : undefined[]
2525

0 commit comments

Comments
 (0)