From 098cceb6d07d253e22f402eb720a9892a4f1f264 Mon Sep 17 00:00:00 2001 From: Kyle Lin Date: Tue, 8 Oct 2019 16:24:50 -0700 Subject: [PATCH] Add type definitions for array.prototype.flat (#38906) --- .../array.prototype.flat-tests.ts | 75 ++++++++ types/array.prototype.flat/auto.d.ts | 162 ++++++++++++++++++ .../array.prototype.flat/implementation.d.ts | 112 ++++++++++++ types/array.prototype.flat/index.d.ts | 18 ++ types/array.prototype.flat/polyfill.d.ts | 4 + types/array.prototype.flat/shim.d.ts | 4 + types/array.prototype.flat/tsconfig.json | 23 +++ types/array.prototype.flat/tslint.json | 1 + 8 files changed, 399 insertions(+) create mode 100644 types/array.prototype.flat/array.prototype.flat-tests.ts create mode 100644 types/array.prototype.flat/auto.d.ts create mode 100644 types/array.prototype.flat/implementation.d.ts create mode 100644 types/array.prototype.flat/index.d.ts create mode 100644 types/array.prototype.flat/polyfill.d.ts create mode 100644 types/array.prototype.flat/shim.d.ts create mode 100644 types/array.prototype.flat/tsconfig.json create mode 100644 types/array.prototype.flat/tslint.json diff --git a/types/array.prototype.flat/array.prototype.flat-tests.ts b/types/array.prototype.flat/array.prototype.flat-tests.ts new file mode 100644 index 0000000000..c25185e53b --- /dev/null +++ b/types/array.prototype.flat/array.prototype.flat-tests.ts @@ -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>>>> = [[[[['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; diff --git a/types/array.prototype.flat/auto.d.ts b/types/array.prototype.flat/auto.d.ts new file mode 100644 index 0000000000..4112932fb6 --- /dev/null +++ b/types/array.prototype.flat/auto.d.ts @@ -0,0 +1,162 @@ +// Adapted from https://github.com/microsoft/TypeScript/blob/master/lib/lib.es2019.array.d.ts + +interface ReadonlyArray { + /** + * 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( + this: + | ReadonlyArray + | ReadonlyArray> + | ReadonlyArray>> + | ReadonlyArray>>> + | ReadonlyArray>>>> + | ReadonlyArray>> + | ReadonlyArray>>>> + | ReadonlyArray>>>> + | ReadonlyArray>>>> + | ReadonlyArray>>> + | ReadonlyArray>>> + | ReadonlyArray>>> + | ReadonlyArray>>>> + | ReadonlyArray>>>> + | ReadonlyArray>>>> + | ReadonlyArray>>>>, + 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( + this: + | ReadonlyArray + | ReadonlyArray>>> + | ReadonlyArray>> + | ReadonlyArray> + | ReadonlyArray>> + | ReadonlyArray>>> + | ReadonlyArray>>> + | ReadonlyArray>>>, + 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( + this: + | ReadonlyArray + | ReadonlyArray> + | ReadonlyArray>> + | ReadonlyArray>>, + 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(this: ReadonlyArray | ReadonlyArray>, 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(this: ReadonlyArray, 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 { + /** + * 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(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(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(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(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(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(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(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(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[]; +} diff --git a/types/array.prototype.flat/implementation.d.ts b/types/array.prototype.flat/implementation.d.ts new file mode 100644 index 0000000000..f1ca161404 --- /dev/null +++ b/types/array.prototype.flat/implementation.d.ts @@ -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(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(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(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( + receiver: + | ReadonlyArray + | ReadonlyArray> + | ReadonlyArray>> + | ReadonlyArray>>> + | ReadonlyArray>>>> + | ReadonlyArray>> + | ReadonlyArray>>>> + | ReadonlyArray>>>> + | ReadonlyArray>>>> + | ReadonlyArray>>> + | ReadonlyArray>>> + | ReadonlyArray>>> + | ReadonlyArray>>>> + | ReadonlyArray>>>> + | ReadonlyArray>>>> + | ReadonlyArray>>>>, + 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( + receiver: + | ReadonlyArray + | ReadonlyArray>>> + | ReadonlyArray>> + | ReadonlyArray> + | ReadonlyArray>> + | ReadonlyArray>>> + | ReadonlyArray>>> + | ReadonlyArray>>>, + 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( + receiver: + | ReadonlyArray + | ReadonlyArray> + | ReadonlyArray>> + | ReadonlyArray>>, + 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(receiver: ReadonlyArray | ReadonlyArray>, 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(receiver: ReadonlyArray, 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, depth?: number): any[]; + +export = flat; diff --git a/types/array.prototype.flat/index.d.ts b/types/array.prototype.flat/index.d.ts new file mode 100644 index 0000000000..80cc366820 --- /dev/null +++ b/types/array.prototype.flat/index.d.ts @@ -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 +// 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; diff --git a/types/array.prototype.flat/polyfill.d.ts b/types/array.prototype.flat/polyfill.d.ts new file mode 100644 index 0000000000..40917f27b4 --- /dev/null +++ b/types/array.prototype.flat/polyfill.d.ts @@ -0,0 +1,4 @@ +import flat = require('./implementation'); + +declare function getPolyfill(): typeof flat; +export = getPolyfill; diff --git a/types/array.prototype.flat/shim.d.ts b/types/array.prototype.flat/shim.d.ts new file mode 100644 index 0000000000..ab73a58d90 --- /dev/null +++ b/types/array.prototype.flat/shim.d.ts @@ -0,0 +1,4 @@ +import flat = require('./implementation'); + +declare function shim(): typeof flat; +export = shim; diff --git a/types/array.prototype.flat/tsconfig.json b/types/array.prototype.flat/tsconfig.json new file mode 100644 index 0000000000..03afe8c010 --- /dev/null +++ b/types/array.prototype.flat/tsconfig.json @@ -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" + ] +} diff --git a/types/array.prototype.flat/tslint.json b/types/array.prototype.flat/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/array.prototype.flat/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }