From c09fe15763fbbfeb9d279c52678e26f27e6bab00 Mon Sep 17 00:00:00 2001 From: Masa-Shin <34234442+Masa-Shin@users.noreply.github.com> Date: Sun, 11 Sep 2022 06:02:46 +0900 Subject: [PATCH] Add readonly array support for some packages (#496) * improve type definition of array-group-by * improve type definition of array-flatten * improve type definition of array-index --- packages/array-flatten/index.d.ts | 4 ++-- packages/array-flatten/index.tests.ts | 11 ++++++----- packages/array-group-by/index.d.ts | 2 +- packages/array-group-by/index.tests.ts | 3 ++- packages/array-index/index.d.ts | 2 +- packages/array-index/index.tests.ts | 9 +++++---- 6 files changed, 17 insertions(+), 14 deletions(-) diff --git a/packages/array-flatten/index.d.ts b/packages/array-flatten/index.d.ts index 202f80f78..11745a815 100644 --- a/packages/array-flatten/index.d.ts +++ b/packages/array-flatten/index.d.ts @@ -1,4 +1,4 @@ -type RecursiveList = (T | T[] | RecursiveList)[]; +type RecursiveList = readonly (T | readonly T[] | RecursiveList)[]; /** * Flattens an array @@ -11,5 +11,5 @@ type RecursiveList = (T | T[] | RecursiveList)[]; * flatten([[1, [2, 3]], [[4, 5], 6, 7, [8, 9]]], 1); * // => [1, [2, 3], [[4, 5], 6, 7, [8, 9]]] */ -declare function flatten(arr: RecursiveList, depth? : number): T[]; +declare function flatten(arr: RecursiveList, depth?: number): T[]; export default flatten; diff --git a/packages/array-flatten/index.tests.ts b/packages/array-flatten/index.tests.ts index 94c1d2ca6..e290cd66b 100644 --- a/packages/array-flatten/index.tests.ts +++ b/packages/array-flatten/index.tests.ts @@ -1,10 +1,11 @@ import flatten from './index' // OK -flatten([1, 2, 3]); -flatten([1, [2, 3], 4]); -flatten([1, [2, [3]], 4]); -flatten([1, [2, [3]], [[[[[4]]]]]]); +const numbers: readonly number[] = [1, 2, 3] +flatten(numbers); +flatten([1, numbers, 4]); +flatten([1, [2, numbers], 4]); +flatten([1, [2, [3]], [[[[numbers]]]]]); flatten([1, [2, [3]], [[[[[4]]]]]], 2); flatten([1, [2, [3]], [[[[[4]]]]]], undefined); @@ -16,7 +17,7 @@ flatten(true); // @ts-expect-error flatten(null); // @ts-expect-error -flatten({a: 5}); +flatten({ a: 5 }); // @ts-expect-error flatten([1], true); // @ts-expect-error diff --git a/packages/array-group-by/index.d.ts b/packages/array-group-by/index.d.ts index a4a9921a1..b28e696f3 100644 --- a/packages/array-group-by/index.d.ts +++ b/packages/array-group-by/index.d.ts @@ -4,6 +4,6 @@ * @param resolver function used to resolve group key */ export default function groupBy( - arr: T[], + arr: readonly T[], resolver: (arg: T) => G ): Record; diff --git a/packages/array-group-by/index.tests.ts b/packages/array-group-by/index.tests.ts index b73b4a843..ed7f4e083 100644 --- a/packages/array-group-by/index.tests.ts +++ b/packages/array-group-by/index.tests.ts @@ -1,7 +1,8 @@ import groupBy from './index'; // OK -const test1: { [key: string]: number[] } = groupBy([6.1, 4.2, 6.3], Math.floor); +const numbers: readonly number[] = [6.1, 4.2, 6.3] +const test1: { [key: string]: number[] } = groupBy(numbers, Math.floor); const test2: { [key: string]: string[] } = groupBy( ['a', 'b', 'c', 'aa', 'bb', 'cc'], str => str.charAt(0) diff --git a/packages/array-index/index.d.ts b/packages/array-index/index.d.ts index c8a693843..fdd4ca22c 100644 --- a/packages/array-index/index.d.ts +++ b/packages/array-index/index.d.ts @@ -14,5 +14,5 @@ * index([{id: 0, val: 0}, {id: 0, val: 1}], 'id'); * // => {first: {id: 0, val: 1}} */ -declare function index(arr: Array, key: string): Record; +declare function index(arr: readonly (T | null | undefined)[], key: string): Record; export default index; diff --git a/packages/array-index/index.tests.ts b/packages/array-index/index.tests.ts index 79b3e20e3..bb4fcb3e0 100644 --- a/packages/array-index/index.tests.ts +++ b/packages/array-index/index.tests.ts @@ -1,13 +1,14 @@ import index from './index' // OK -index([ { a: 5 }, { a: 6 } ], 'a'); -index([ { a: 5 }, { a: 6 }, null, undefined ], 'a'); -index([ { a: 5 }, { a: 6 } ], 'b'); +const objects: readonly Record[] = [{ a: 5 }, { a: 6 }] +index(objects, 'a'); +index([{ a: 5 }, { a: 6 }, null, undefined], 'a'); +index([{ a: 5 }, { a: 6 }], 'b'); index([1, 2, 3], 'a'); // Not OK // @ts-expect-error -index([ { a: 5 }, { a: 6 } ], (o) => o.a); +index([{ a: 5 }, { a: 6 }], (o) => o.a); // @ts-expect-error index({ a: 6 }, 'a');