Skip to content

Fix unsound Array.flatMap() typings #33630

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 2 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
8 changes: 4 additions & 4 deletions src/lib/es2019.array.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ interface ReadonlyArray<T> {
* 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>,
callback: (this: This, value: T, index: number, array: T[]) => U,
thisArg?: This
): U[]
): (U extends readonly (infer V)[] ? V : U)[];


/**
Expand Down Expand Up @@ -125,9 +125,9 @@ interface Array<T> {
* 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>,
callback: (this: This, value: T, index: number, array: T[]) => U,
thisArg?: This
): U[]
): (U extends readonly (infer V)[] ? V : U)[];

/**
* Returns a new array with all sub-array elements concatenated into it recursively up to the
Expand Down
8 changes: 8 additions & 0 deletions tests/baselines/reference/arrayFlatMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,18 @@ const array: number[] = [];
const readonlyArray: ReadonlyArray<number> = [];
array.flatMap((): ReadonlyArray<number> => []); // ok
readonlyArray.flatMap((): ReadonlyArray<number> => []); // ok

// #19535

const [x] = [""].flatMap(undefined as () => string[] | string[][]);
x == "";


//// [arrayFlatMap.js]
var array = [];
var readonlyArray = [];
array.flatMap(function () { return []; }); // ok
readonlyArray.flatMap(function () { return []; }); // ok
// #19535
var x = [""].flatMap(undefined)[0];
x == "";
11 changes: 11 additions & 0 deletions tests/baselines/reference/arrayFlatMap.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,14 @@ readonlyArray.flatMap((): ReadonlyArray<number> => []); // ok
>flatMap : Symbol(ReadonlyArray.flatMap, Decl(lib.es2019.array.d.ts, --, --))
>ReadonlyArray : Symbol(ReadonlyArray, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.core.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2016.array.include.d.ts, --, --), Decl(lib.es2019.array.d.ts, --, --))

// #19535

const [x] = [""].flatMap(undefined as () => string[] | string[][]);
>x : Symbol(x, Decl(arrayFlatMap.ts, 7, 7))
>[""].flatMap : Symbol(Array.flatMap, Decl(lib.es2019.array.d.ts, --, --))
>flatMap : Symbol(Array.flatMap, Decl(lib.es2019.array.d.ts, --, --))
>undefined : Symbol(undefined)

x == "";
>x : Symbol(x, Decl(arrayFlatMap.ts, 7, 7))

25 changes: 21 additions & 4 deletions tests/baselines/reference/arrayFlatMap.types
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,34 @@ const readonlyArray: ReadonlyArray<number> = [];

array.flatMap((): ReadonlyArray<number> => []); // ok
>array.flatMap((): ReadonlyArray<number> => []) : number[]
>array.flatMap : <U, This = undefined>(callback: (this: This, value: number, index: number, array: number[]) => U | readonly U[], thisArg?: This) => U[]
>array.flatMap : <U, This = undefined>(callback: (this: This, value: number, index: number, array: number[]) => U, thisArg?: This) => (U extends readonly (infer V)[] ? V : U)[]
>array : number[]
>flatMap : <U, This = undefined>(callback: (this: This, value: number, index: number, array: number[]) => U | readonly U[], thisArg?: This) => U[]
>flatMap : <U, This = undefined>(callback: (this: This, value: number, index: number, array: number[]) => U, thisArg?: This) => (U extends readonly (infer V)[] ? V : U)[]
>(): ReadonlyArray<number> => [] : () => readonly number[]
>[] : undefined[]

readonlyArray.flatMap((): ReadonlyArray<number> => []); // ok
>readonlyArray.flatMap((): ReadonlyArray<number> => []) : number[]
>readonlyArray.flatMap : <U, This = undefined>(callback: (this: This, value: number, index: number, array: number[]) => U | readonly U[], thisArg?: This) => U[]
>readonlyArray.flatMap : <U, This = undefined>(callback: (this: This, value: number, index: number, array: number[]) => U, thisArg?: This) => (U extends readonly (infer V)[] ? V : U)[]
>readonlyArray : readonly number[]
>flatMap : <U, This = undefined>(callback: (this: This, value: number, index: number, array: number[]) => U | readonly U[], thisArg?: This) => U[]
>flatMap : <U, This = undefined>(callback: (this: This, value: number, index: number, array: number[]) => U, thisArg?: This) => (U extends readonly (infer V)[] ? V : U)[]
>(): ReadonlyArray<number> => [] : () => readonly number[]
>[] : undefined[]

// #19535

const [x] = [""].flatMap(undefined as () => string[] | string[][]);
>x : string | string[]
>[""].flatMap(undefined as () => string[] | string[][]) : (string | string[])[]
>[""].flatMap : <U, This = undefined>(callback: (this: This, value: string, index: number, array: string[]) => U, thisArg?: This) => (U extends readonly (infer V)[] ? V : U)[]
>[""] : string[]
>"" : ""
>flatMap : <U, This = undefined>(callback: (this: This, value: string, index: number, array: string[]) => U, thisArg?: This) => (U extends readonly (infer V)[] ? V : U)[]
>undefined as () => string[] | string[][] : () => string[] | string[][]
>undefined : undefined

x == "";
>x == "" : boolean
>x : string | string[]
>"" : ""

5 changes: 5 additions & 0 deletions tests/cases/compiler/arrayFlatMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,8 @@ const array: number[] = [];
const readonlyArray: ReadonlyArray<number> = [];
array.flatMap((): ReadonlyArray<number> => []); // ok
readonlyArray.flatMap((): ReadonlyArray<number> => []); // ok

// #19535

const [x] = [""].flatMap(undefined as () => string[] | string[][]);
x == "";