🤖 Merge PR #46399 array.prototype.flatmap: support ReadonlyArray by @mpartel

* array.prototype.flatmap: support ReadonlyArray

* array.prototype.flatmap: support readonly result from callback
This commit is contained in:
Martin Pärtel 2020-07-29 09:27:37 +03:00 committed by GitHub
parent b27845dbea
commit 59020cc50a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 19 additions and 3 deletions

View File

@ -45,3 +45,12 @@ getPolyfill()(["foo"], word => word.split("")); // $ExpectType string[]
// `shim` installs a flatMap implementation in `Array` prototype and returns it
shim()(["foo"], word => word.split("")); // $ExpectType string[]
// `ReadonlyArray` is supported
(["foo"] as ReadonlyArray<string>).flatMap(word => word.split("")); // $ExpectType string[]
// Readonly result from callback is supported
flatMap([[1], [2]], a => a as ReadonlyArray<number>); // $ExpectType number[]
flatMap([[1], [2]] as ReadonlyArray<ReadonlyArray<number>>, a => a); // $ExpectType number[]
([[1], [2]]).flatMap(a => a as ReadonlyArray<number>); // $ExpectType number[]
([[1], [2]] as ReadonlyArray<ReadonlyArray<number>>).flatMap(a => a); // $ExpectType number[]

View File

@ -1,6 +1,13 @@
interface Array<T> {
flatMap<U, R extends object | undefined = undefined>(
fn: (this: R, x: T, index: number, array: this) => U[],
fn: (this: R, x: T, index: number, array: this) => ReadonlyArray<U>,
thisArg?: R
): U[];
}
interface ReadonlyArray<T> {
flatMap<U, R extends object | undefined = undefined>(
fn: (this: R, x: T, index: number, array: this) => ReadonlyArray<U>,
thisArg?: R
): U[];
}

View File

@ -1,7 +1,7 @@
// This is the same type as the callable signature in `FlatMap` in `index.d.ts`.
declare function flatMap<A, B, T extends object | undefined = undefined>(
xs: ReadonlyArray<A>,
fn: (this: T, x: A, index: number, array: A[]) => B[],
fn: (this: T, x: A, index: number, array: A[]) => ReadonlyArray<B>,
thisArg?: T
): B[];
export = flatMap;

View File

@ -10,7 +10,7 @@ import flatMapImpl = require("./implementation");
interface FlatMap {
<A, B, T extends object | undefined = undefined>(
xs: ReadonlyArray<A>,
fn: (this: T, x: A, index: number, array: A[]) => B[],
fn: (this: T, x: A, index: number, array: A[]) => ReadonlyArray<B>,
thisArg?: T
): B[];
getPolyfill(): typeof flatMapImpl;