From 59020cc50ac0888a561fe2ff012115ddae6b1e47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20P=C3=A4rtel?= Date: Wed, 29 Jul 2020 09:27:37 +0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=A4=96=20Merge=20PR=20#46399=20array.prot?= =?UTF-8?q?otype.flatmap:=20support=20ReadonlyArray=20by=20@mpartel?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * array.prototype.flatmap: support ReadonlyArray * array.prototype.flatmap: support readonly result from callback --- .../array.prototype.flatmap-tests.ts | 9 +++++++++ types/array.prototype.flatmap/auto.d.ts | 9 ++++++++- types/array.prototype.flatmap/implementation.d.ts | 2 +- types/array.prototype.flatmap/index.d.ts | 2 +- 4 files changed, 19 insertions(+), 3 deletions(-) diff --git a/types/array.prototype.flatmap/array.prototype.flatmap-tests.ts b/types/array.prototype.flatmap/array.prototype.flatmap-tests.ts index 5236a352ca..e672ebf688 100644 --- a/types/array.prototype.flatmap/array.prototype.flatmap-tests.ts +++ b/types/array.prototype.flatmap/array.prototype.flatmap-tests.ts @@ -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).flatMap(word => word.split("")); // $ExpectType string[] + +// Readonly result from callback is supported +flatMap([[1], [2]], a => a as ReadonlyArray); // $ExpectType number[] +flatMap([[1], [2]] as ReadonlyArray>, a => a); // $ExpectType number[] +([[1], [2]]).flatMap(a => a as ReadonlyArray); // $ExpectType number[] +([[1], [2]] as ReadonlyArray>).flatMap(a => a); // $ExpectType number[] diff --git a/types/array.prototype.flatmap/auto.d.ts b/types/array.prototype.flatmap/auto.d.ts index 43177fb3ad..063e6ed800 100644 --- a/types/array.prototype.flatmap/auto.d.ts +++ b/types/array.prototype.flatmap/auto.d.ts @@ -1,6 +1,13 @@ interface Array { flatMap( - fn: (this: R, x: T, index: number, array: this) => U[], + fn: (this: R, x: T, index: number, array: this) => ReadonlyArray, + thisArg?: R + ): U[]; +} + +interface ReadonlyArray { + flatMap( + fn: (this: R, x: T, index: number, array: this) => ReadonlyArray, thisArg?: R ): U[]; } diff --git a/types/array.prototype.flatmap/implementation.d.ts b/types/array.prototype.flatmap/implementation.d.ts index 80ad9f39d0..e2892d17a0 100644 --- a/types/array.prototype.flatmap/implementation.d.ts +++ b/types/array.prototype.flatmap/implementation.d.ts @@ -1,7 +1,7 @@ // This is the same type as the callable signature in `FlatMap` in `index.d.ts`. declare function flatMap( xs: ReadonlyArray, - fn: (this: T, x: A, index: number, array: A[]) => B[], + fn: (this: T, x: A, index: number, array: A[]) => ReadonlyArray, thisArg?: T ): B[]; export = flatMap; diff --git a/types/array.prototype.flatmap/index.d.ts b/types/array.prototype.flatmap/index.d.ts index fe3b53a914..6180272385 100644 --- a/types/array.prototype.flatmap/index.d.ts +++ b/types/array.prototype.flatmap/index.d.ts @@ -10,7 +10,7 @@ import flatMapImpl = require("./implementation"); interface FlatMap { ( xs: ReadonlyArray, - fn: (this: T, x: A, index: number, array: A[]) => B[], + fn: (this: T, x: A, index: number, array: A[]) => ReadonlyArray, thisArg?: T ): B[]; getPolyfill(): typeof flatMapImpl;