mirror of
https://github.com/zhigang1992/DefinitelyTyped.git
synced 2026-04-24 05:06:02 +08:00
Type definitions for array.prototype.flatmap (#29437)
* Type definitions for array.prototype.flatmap * array.prototype.flatmap: importing "/auto" extends the Array prototype
This commit is contained in:
committed by
Wesley Wigham
parent
aef895f924
commit
1899725695
@@ -0,0 +1,47 @@
|
||||
import flatMap = require("array.prototype.flatmap");
|
||||
import "array.prototype.flatmap/auto";
|
||||
import flatMapImpl = require("array.prototype.flatmap/implementation");
|
||||
import getPolyfill = require("array.prototype.flatmap/polyfill");
|
||||
import shim = require("array.prototype.flatmap/shim");
|
||||
|
||||
// infers type of the output array from the return type of the callback
|
||||
flatMap(["foo"], word => word.split("")); // $ExpectType string[]
|
||||
flatMapImpl(["foo"], word => word.split("")); // $ExpectType string[]
|
||||
["foo"].flatMap(word => word.split("")); // $ExpectType string[]
|
||||
|
||||
// infers the type of the value argument to the callback
|
||||
flatMap([1, 2], word => word.split("")); // $ExpectError
|
||||
flatMapImpl([1, 2], word => word.split("")); // $ExpectError
|
||||
[1, 2].flatMap(word => word.split("")); // $ExpectError
|
||||
|
||||
// the callback must return an array
|
||||
flatMap([1, 2], word => word); // $ExpectError
|
||||
flatMapImpl([1, 2], word => word); // $ExpectError
|
||||
[1, 2].flatMap(word => word); // $ExpectError
|
||||
|
||||
// the callback accepts an index argument
|
||||
flatMap(["foo"], (_, index) => [index]); // $ExpectType number[]
|
||||
flatMapImpl(["foo"], (_, index) => [index]); // $ExpectType number[]
|
||||
["foo"].flatMap((_, index) => [index]); // $ExpectType number[]
|
||||
|
||||
// the callback accepts an argument that refers to the original array
|
||||
flatMap(["foo"], (_, __, input) => input); // $ExpectType string[]
|
||||
flatMapImpl(["foo"], (_, __, input) => input); // $ExpectType string[]
|
||||
["foo"].flatMap((_, __, input) => input); // $ExpectType string[]
|
||||
|
||||
// the third argument is used as the calling context for the callback
|
||||
flatMap(["foo"], function() { return this.foo; }, { foo: [1, 2] }); // $ExpectType number[]
|
||||
flatMapImpl(["foo"], function() { return this.foo; }, { foo: [1, 2] }); // $ExpectType number[]
|
||||
["foo"].flatMap(function() { return this.foo; }, { foo: [1, 2] }); // $ExpectType number[]
|
||||
|
||||
// assumes that value of `this` in callback is `undefined` by default (this is
|
||||
// accurate in strict mode)
|
||||
flatMap([1], function() { return [this]; }); // $ExpectType undefined[]
|
||||
flatMapImpl([1], function() { return [this]; }); // $ExpectType undefined[]
|
||||
[1].flatMap(function() { return [this]; }); // $ExpectType undefined[]
|
||||
|
||||
// `getPolyfill` returns a flatMap implementation
|
||||
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[]
|
||||
6
types/array.prototype.flatmap/auto.d.ts
vendored
Normal file
6
types/array.prototype.flatmap/auto.d.ts
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
interface Array<T> {
|
||||
flatMap<U, R extends object | undefined = undefined>(
|
||||
fn: (this: R, x: T, index: number, array: this) => U[],
|
||||
thisArg?: R
|
||||
): U[];
|
||||
}
|
||||
7
types/array.prototype.flatmap/implementation.d.ts
vendored
Normal file
7
types/array.prototype.flatmap/implementation.d.ts
vendored
Normal file
@@ -0,0 +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[],
|
||||
thisArg?: T
|
||||
): B[];
|
||||
export = flatMap;
|
||||
21
types/array.prototype.flatmap/index.d.ts
vendored
Normal file
21
types/array.prototype.flatmap/index.d.ts
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
// Type definitions for array.prototype.flatmap 1.2
|
||||
// Project: https://github.com/es-shims/Array.prototype.flatMap#readme
|
||||
// Definitions by: Jesse Hallett <https://github.com/hallettj>
|
||||
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
|
||||
// TypeScript Version: 2.3
|
||||
|
||||
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[],
|
||||
thisArg?: T
|
||||
): B[];
|
||||
getPolyfill(): typeof flatMapImpl;
|
||||
implementation: typeof flatMapImpl;
|
||||
shim(): typeof flatMapImpl;
|
||||
}
|
||||
|
||||
declare const flatMap: FlatMap;
|
||||
export = flatMap;
|
||||
4
types/array.prototype.flatmap/polyfill.d.ts
vendored
Normal file
4
types/array.prototype.flatmap/polyfill.d.ts
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
import flatMap = require("./implementation");
|
||||
|
||||
declare function getPolyfill(): typeof flatMap;
|
||||
export = getPolyfill;
|
||||
4
types/array.prototype.flatmap/shim.d.ts
vendored
Normal file
4
types/array.prototype.flatmap/shim.d.ts
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
import flatMap = require("./implementation");
|
||||
|
||||
declare function shim(): typeof flatMap;
|
||||
export = shim;
|
||||
27
types/array.prototype.flatmap/tsconfig.json
Normal file
27
types/array.prototype.flatmap/tsconfig.json
Normal file
@@ -0,0 +1,27 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"module": "commonjs",
|
||||
"lib": [
|
||||
"es6"
|
||||
],
|
||||
"noImplicitAny": true,
|
||||
"noImplicitThis": true,
|
||||
"strictNullChecks": true,
|
||||
"strictFunctionTypes": true,
|
||||
"baseUrl": "../",
|
||||
"typeRoots": [
|
||||
"../"
|
||||
],
|
||||
"types": [],
|
||||
"noEmit": true,
|
||||
"forceConsistentCasingInFileNames": true
|
||||
},
|
||||
"files": [
|
||||
"array.prototype.flatmap-tests.ts",
|
||||
"auto.d.ts",
|
||||
"implementation.d.ts",
|
||||
"index.d.ts",
|
||||
"polyfill.d.ts",
|
||||
"shim.d.ts"
|
||||
]
|
||||
}
|
||||
1
types/array.prototype.flatmap/tslint.json
Normal file
1
types/array.prototype.flatmap/tslint.json
Normal file
@@ -0,0 +1 @@
|
||||
{ "extends": "dtslint/dt.json" }
|
||||
Reference in New Issue
Block a user