Add type definitions for array.prototype.flat (#38906)

This commit is contained in:
Kyle Lin
2019-10-08 16:24:50 -07:00
committed by Armando Aguirre
parent 9d8c69e5e0
commit 098cceb6d0
8 changed files with 399 additions and 0 deletions

View File

@@ -0,0 +1,75 @@
import flat = require('array.prototype.flat');
import 'array.prototype.flat/auto';
import flatImpl = require('array.prototype.flat/implementation');
import getPolyfill = require('array.prototype.flat/polyfill');
import shim = require('array.prototype.flat/shim');
// Infers nesting-level of the output array from
// the depth argument up to a depth of 4.
flat(['foo'], 0); // $ExpectType string[]
flat([['foo']], 1); // $ExpectType string[]
flat([['foo']], 0); // $ExpectType string[][]
flat([[['foo']]], 2); // $ExpectType string[]
flat([[['foo']]], 1); // $ExpectType string[][]
flat([[['foo']]], 0); // $ExpectType string[][][]
flat([[[['foo']]]], 3); // $ExpectType string[]
flat([[[['foo']]]], 2); // $ExpectType string[][]
flat([[[['foo']]]], 1); // $ExpectType string[][][]
flat([[[['foo']]]], 0); // $ExpectType string[][][][]
flat([[[[['foo']]]]], 4); // $ExpectType string[]
flat([[[[['foo']]]]], 3); // $ExpectType string[][]
flat([[[[['foo']]]]], 2); // $ExpectType string[][][]
flat([[[[['foo']]]]], 1); // $ExpectType string[][][][]
flat([[[[['foo']]]]], 0); // $ExpectType string[][][][][]
flatImpl(['foo'], 0); // $ExpectType string[]
flatImpl([['foo']], 1); // $ExpectType string[]
flatImpl([['foo']], 0); // $ExpectType string[][]
flatImpl([[['foo']]], 2); // $ExpectType string[]
flatImpl([[['foo']]], 1); // $ExpectType string[][]
flatImpl([[['foo']]], 0); // $ExpectType string[][][]
flatImpl([[[['foo']]]], 3); // $ExpectType string[]
flatImpl([[[['foo']]]], 2); // $ExpectType string[][]
flatImpl([[[['foo']]]], 1); // $ExpectType string[][][]
flatImpl([[[['foo']]]], 0); // $ExpectType string[][][][]
flatImpl([[[[['foo']]]]], 4); // $ExpectType string[]
flatImpl([[[[['foo']]]]], 3); // $ExpectType string[][]
flatImpl([[[[['foo']]]]], 2); // $ExpectType string[][][]
flatImpl([[[[['foo']]]]], 1); // $ExpectType string[][][][]
flatImpl([[[[['foo']]]]], 0); // $ExpectType string[][][][][]
['foo'].flat(0); // $ExpectType string[]
[['foo']].flat(1); // $ExpectType string[]
[['foo']].flat(0); // $ExpectType string[][]
[[['foo']]].flat(2); // $ExpectType string[]
[[['foo']]].flat(1); // $ExpectType string[][]
[[['foo']]].flat(0); // $ExpectType string[][][]
[[[['foo']]]].flat(3); // $ExpectType string[]
[[[['foo']]]].flat(2); // $ExpectType string[][]
[[[['foo']]]].flat(1); // $ExpectType string[][][]
[[[['foo']]]].flat(0); // $ExpectType string[][][][]
[[[[['foo']]]]].flat(4); // $ExpectType string[]
[[[[['foo']]]]].flat(3); // $ExpectType string[][]
[[[[['foo']]]]].flat(2); // $ExpectType string[][][]
[[[[['foo']]]]].flat(1); // $ExpectType string[][][][]
[[[[['foo']]]]].flat(0); // $ExpectType string[][][][][]
// Supports `ReadonlyArray` up to a depth of 4.
const readOnly: ReadonlyArray<ReadonlyArray<ReadonlyArray<ReadonlyArray<ReadonlyArray<string>>>>> = [[[[['foo']]]]];
flat(readOnly, 4); // $ExpectType string[]
// Supports `Array` up to a depth of 7.
const mutable: string[][][][][][][][] = [[[[[[[['string']]]]]]]];
flat(mutable, 7); // $ExpectType string[]
mutable.flat(7); // $ExpectType string[]
// `getPolyfill` returns a flat implementation
getPolyfill()([['foo']], 1); // $ExpectType string[]
// `shim` installs a flat implementation in `Array` prototype and returns it
shim()([['foo']], 1); // $ExpectType string[]
// `getPolyfill`, `implementation`, and `shim` are properties of the `flat` function.
const _getPolyfill: typeof getPolyfill = flat.getPolyfill;
const _implementation: typeof flatImpl = flat.implementation;
const _shim: typeof shim = flat.shim;

162
types/array.prototype.flat/auto.d.ts vendored Normal file
View File

@@ -0,0 +1,162 @@
// Adapted from https://github.com/microsoft/TypeScript/blob/master/lib/lib.es2019.array.d.ts
interface ReadonlyArray<T> {
/**
* Returns a new array with all sub-array elements concatenated into it recursively up to the
* specified depth.
*
* @param depth The maximum recursion depth
*/
flat<U>(
this:
| ReadonlyArray<U[][][][]>
| ReadonlyArray<ReadonlyArray<U[][][]>>
| ReadonlyArray<Array<ReadonlyArray<U[][]>>>
| ReadonlyArray<Array<Array<ReadonlyArray<U[]>>>>
| ReadonlyArray<Array<Array<Array<ReadonlyArray<U>>>>>
| ReadonlyArray<ReadonlyArray<ReadonlyArray<U[][]>>>
| ReadonlyArray<ReadonlyArray<Array<Array<ReadonlyArray<U>>>>>
| ReadonlyArray<ReadonlyArray<Array<Array<ReadonlyArray<U>>>>>
| ReadonlyArray<ReadonlyArray<Array<Array<ReadonlyArray<U>>>>>
| ReadonlyArray<ReadonlyArray<Array<ReadonlyArray<U[]>>>>
| ReadonlyArray<ReadonlyArray<Array<ReadonlyArray<U[]>>>>
| ReadonlyArray<ReadonlyArray<ReadonlyArray<ReadonlyArray<U[]>>>>
| ReadonlyArray<ReadonlyArray<ReadonlyArray<Array<ReadonlyArray<U>>>>>
| ReadonlyArray<ReadonlyArray<ReadonlyArray<Array<ReadonlyArray<U>>>>>
| ReadonlyArray<Array<ReadonlyArray<ReadonlyArray<ReadonlyArray<U>>>>>
| ReadonlyArray<ReadonlyArray<ReadonlyArray<ReadonlyArray<ReadonlyArray<U>>>>>,
depth: 4,
): U[];
/**
* Returns a new array with all sub-array elements concatenated into it recursively up to the
* specified depth.
*
* @param depth The maximum recursion depth
*/
flat<U>(
this:
| ReadonlyArray<U[][][]>
| ReadonlyArray<Array<Array<ReadonlyArray<U>>>>
| ReadonlyArray<Array<ReadonlyArray<U[]>>>
| ReadonlyArray<ReadonlyArray<U[][]>>
| ReadonlyArray<ReadonlyArray<ReadonlyArray<U[]>>>
| ReadonlyArray<ReadonlyArray<Array<ReadonlyArray<U>>>>
| ReadonlyArray<ReadonlyArray<Array<ReadonlyArray<U>>>>
| ReadonlyArray<ReadonlyArray<ReadonlyArray<ReadonlyArray<U>>>>,
depth: 3,
): U[];
/**
* Returns a new array with all sub-array elements concatenated into it recursively up to the
* specified depth.
*
* @param depth The maximum recursion depth
*/
flat<U>(
this:
| ReadonlyArray<U[][]>
| ReadonlyArray<ReadonlyArray<U[]>>
| ReadonlyArray<Array<ReadonlyArray<U>>>
| ReadonlyArray<ReadonlyArray<ReadonlyArray<U>>>,
depth: 2,
): U[];
/**
* Returns a new array with all sub-array elements concatenated into it recursively up to the
* specified depth.
*
* @param depth The maximum recursion depth
*/
flat<U>(this: ReadonlyArray<U[]> | ReadonlyArray<ReadonlyArray<U>>, depth?: 1): U[];
/**
* Returns a new array with all sub-array elements concatenated into it recursively up to the
* specified depth.
*
* @param depth The maximum recursion depth
*/
flat<U>(this: ReadonlyArray<U>, depth: 0): U[];
/**
* Returns a new array with all sub-array elements concatenated into it recursively up to the
* specified depth. If no depth is provided, flat method defaults to the depth of 1.
*
* @param depth The maximum recursion depth
*/
flat(depth?: number): any[];
}
interface Array<T> {
/**
* Returns a new array with all sub-array elements concatenated into it recursively up to the
* specified depth.
*
* @param depth The maximum recursion depth
*/
flat<U>(this: U[][][][][][][][], depth: 7): U[];
/**
* Returns a new array with all sub-array elements concatenated into it recursively up to the
* specified depth.
*
* @param depth The maximum recursion depth
*/
flat<U>(this: U[][][][][][][], depth: 6): U[];
/**
* Returns a new array with all sub-array elements concatenated into it recursively up to the
* specified depth.
*
* @param depth The maximum recursion depth
*/
flat<U>(this: U[][][][][][], depth: 5): U[];
/**
* Returns a new array with all sub-array elements concatenated into it recursively up to the
* specified depth.
*
* @param depth The maximum recursion depth
*/
flat<U>(this: U[][][][][], depth: 4): U[];
/**
* Returns a new array with all sub-array elements concatenated into it recursively up to the
* specified depth.
*
* @param depth The maximum recursion depth
*/
flat<U>(this: U[][][][], depth: 3): U[];
/**
* Returns a new array with all sub-array elements concatenated into it recursively up to the
* specified depth.
*
* @param depth The maximum recursion depth
*/
flat<U>(this: U[][][], depth: 2): U[];
/**
* Returns a new array with all sub-array elements concatenated into it recursively up to the
* specified depth.
*
* @param depth The maximum recursion depth
*/
flat<U>(this: U[][], depth?: 1): U[];
/**
* Returns a new array with all sub-array elements concatenated into it recursively up to the
* specified depth.
*
* @param depth The maximum recursion depth
*/
flat<U>(this: U[], depth: 0): U[];
/**
* Returns a new array with all sub-array elements concatenated into it recursively up to the
* specified depth. If no depth is provided, flat method defaults to the depth of 1.
*
* @param depth The maximum recursion depth
*/
flat(depth?: number): any[];
}

View File

@@ -0,0 +1,112 @@
// Adapted from https://github.com/microsoft/TypeScript/blob/master/lib/lib.es2019.array.d.ts
/**
* Returns a new array with all sub-array elements concatenated into it recursively up to the
* specified depth.
*
* @param depth The maximum recursion depth
*/
declare function flat<U>(receiver: U[][][][][][][][], depth: 7): U[];
/**
* Returns a new array with all sub-array elements concatenated into it recursively up to the
* specified depth.
*
* @param depth The maximum recursion depth
*/
declare function flat<U>(receiver: U[][][][][][][], depth: 6): U[];
/**
* Returns a new array with all sub-array elements concatenated into it recursively up to the
* specified depth.
*
* @param depth The maximum recursion depth
*/
declare function flat<U>(receiver: U[][][][][][], depth: 5): U[];
/**
* Returns a new array with all sub-array elements concatenated into it recursively up to the
* specified depth.
*
* @param depth The maximum recursion depth
*/
declare function flat<U>(
receiver:
| ReadonlyArray<U[][][][]>
| ReadonlyArray<ReadonlyArray<U[][][]>>
| ReadonlyArray<Array<ReadonlyArray<U[][]>>>
| ReadonlyArray<Array<Array<ReadonlyArray<U[]>>>>
| ReadonlyArray<Array<Array<Array<ReadonlyArray<U>>>>>
| ReadonlyArray<ReadonlyArray<ReadonlyArray<U[][]>>>
| ReadonlyArray<ReadonlyArray<Array<Array<ReadonlyArray<U>>>>>
| ReadonlyArray<ReadonlyArray<Array<Array<ReadonlyArray<U>>>>>
| ReadonlyArray<ReadonlyArray<Array<Array<ReadonlyArray<U>>>>>
| ReadonlyArray<ReadonlyArray<Array<ReadonlyArray<U[]>>>>
| ReadonlyArray<ReadonlyArray<Array<ReadonlyArray<U[]>>>>
| ReadonlyArray<ReadonlyArray<ReadonlyArray<ReadonlyArray<U[]>>>>
| ReadonlyArray<ReadonlyArray<ReadonlyArray<Array<ReadonlyArray<U>>>>>
| ReadonlyArray<ReadonlyArray<ReadonlyArray<Array<ReadonlyArray<U>>>>>
| ReadonlyArray<Array<ReadonlyArray<ReadonlyArray<ReadonlyArray<U>>>>>
| ReadonlyArray<ReadonlyArray<ReadonlyArray<ReadonlyArray<ReadonlyArray<U>>>>>,
depth: 4,
): U[];
/**
* Returns a new array with all sub-array elements concatenated into it recursively up to the
* specified depth.
*
* @param depth The maximum recursion depth
*/
declare function flat<U>(
receiver:
| ReadonlyArray<U[][][]>
| ReadonlyArray<Array<Array<ReadonlyArray<U>>>>
| ReadonlyArray<Array<ReadonlyArray<U[]>>>
| ReadonlyArray<ReadonlyArray<U[][]>>
| ReadonlyArray<ReadonlyArray<ReadonlyArray<U[]>>>
| ReadonlyArray<ReadonlyArray<Array<ReadonlyArray<U>>>>
| ReadonlyArray<ReadonlyArray<Array<ReadonlyArray<U>>>>
| ReadonlyArray<ReadonlyArray<ReadonlyArray<ReadonlyArray<U>>>>,
depth: 3,
): U[];
/**
* Returns a new array with all sub-array elements concatenated into it recursively up to the
* specified depth.
*
* @param depth The maximum recursion depth
*/
declare function flat<U>(
receiver:
| ReadonlyArray<U[][]>
| ReadonlyArray<ReadonlyArray<U[]>>
| ReadonlyArray<Array<ReadonlyArray<U>>>
| ReadonlyArray<ReadonlyArray<ReadonlyArray<U>>>,
depth: 2,
): U[];
/**
* Returns a new array with all sub-array elements concatenated into it recursively up to the
* specified depth.
*
* @param depth The maximum recursion depth
*/
declare function flat<U>(receiver: ReadonlyArray<U[]> | ReadonlyArray<ReadonlyArray<U>>, depth?: 1): U[];
/**
* Returns a new array with all sub-array elements concatenated into it recursively up to the
* specified depth.
*
* @param depth The maximum recursion depth
*/
declare function flat<U>(receiver: ReadonlyArray<U>, depth: 0): U[];
/**
* Returns a new array with all sub-array elements concatenated into it recursively up to the
* specified depth. If no depth is provided, flat method defaults to the depth of 1.
*
* @param depth The maximum recursion depth
*/
declare function flat(receiver: ReadonlyArray<any>, depth?: number): any[];
export = flat;

18
types/array.prototype.flat/index.d.ts vendored Normal file
View File

@@ -0,0 +1,18 @@
// Type definitions for array.prototype.flat 1.2
// Project: https://github.com/es-shims/Array.prototype.flat#readme
// Definitions by: Kyle Lin <https://github.com/kylejlin>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.4
import flatImpl = require('./implementation');
type FlatImpl = typeof flatImpl;
interface Flat extends FlatImpl {
getPolyfill(): FlatImpl;
implementation: FlatImpl;
shim(): FlatImpl;
}
declare const flat: Flat;
export = flat;

View File

@@ -0,0 +1,4 @@
import flat = require('./implementation');
declare function getPolyfill(): typeof flat;
export = getPolyfill;

4
types/array.prototype.flat/shim.d.ts vendored Normal file
View File

@@ -0,0 +1,4 @@
import flat = require('./implementation');
declare function shim(): typeof flat;
export = shim;

View File

@@ -0,0 +1,23 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": ["es6"],
"noImplicitAny": true,
"noImplicitThis": true,
"strictFunctionTypes": true,
"strictNullChecks": true,
"baseUrl": "../",
"typeRoots": ["../"],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"array.prototype.flat-tests.ts",
"auto.d.ts",
"implementation.d.ts",
"index.d.ts",
"polyfill.d.ts",
"shim.d.ts"
]
}

View File

@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }