From 4d23fec62606b23070f2caa9f33916984f375c34 Mon Sep 17 00:00:00 2001 From: Masahiko Shin Date: Tue, 25 Apr 2023 16:59:06 +0900 Subject: [PATCH 01/13] Add readonly array support to array-compact --- packages/array-compact/index.d.ts | 2 +- packages/array-compact/index.tests.ts | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/array-compact/index.d.ts b/packages/array-compact/index.d.ts index 31a698987..bec718074 100644 --- a/packages/array-compact/index.d.ts +++ b/packages/array-compact/index.d.ts @@ -9,4 +9,4 @@ type Falsy = false | null | undefined | '' | 0 | 0n; * compact([1, 2, [], 4, {}]); * // => [1, 2, [], 4, {}] */ -export default function compact(arr: (Falsy | T)[]): T[]; +export default function compact(arr: readonly (Falsy | T)[]): T[]; diff --git a/packages/array-compact/index.tests.ts b/packages/array-compact/index.tests.ts index 20df27749..5ae9b9c77 100644 --- a/packages/array-compact/index.tests.ts +++ b/packages/array-compact/index.tests.ts @@ -9,6 +9,9 @@ const test4: bigint[] = compact([-0, null, '', BigInt(0), BigInt(1)]) // [BigInt // Inferred as (string | number)[] because of NaN const test5: (string | number)[] = compact(['a', 'b', 'c', NaN]) // ['a', 'b', 'c'] +const readOnlyArr: readonly (number | boolean)[] = [1, true, false] +compact(readOnlyArr) + // Not OK // @ts-expect-error compact() From 9f7689f8dad4c12619396ca9e0bc57431a11c15b Mon Sep 17 00:00:00 2001 From: Masahiko Shin Date: Tue, 25 Apr 2023 16:59:23 +0900 Subject: [PATCH 02/13] Add readonly array support to array-insert --- packages/array-insert/index.d.ts | 6 +++++- packages/array-insert/index.tests.ts | 3 +++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/packages/array-insert/index.d.ts b/packages/array-insert/index.d.ts index 22510cbcf..397d78237 100644 --- a/packages/array-insert/index.d.ts +++ b/packages/array-insert/index.d.ts @@ -11,4 +11,8 @@ * insert([1, 2, 5, 6], ['a', 'c', 'e'], 2); * // => [1, 2, 'a', 'c', 'e', 5, 6] */ -export default function insert(arr: T[], val: U[] | U, index?: number): (T | U)[] +export default function insert( + arr: readonly T[], + val: readonly U[] | U, + index?: number +): (T | U)[] diff --git a/packages/array-insert/index.tests.ts b/packages/array-insert/index.tests.ts index 6659d4c4d..4eee59fc8 100644 --- a/packages/array-insert/index.tests.ts +++ b/packages/array-insert/index.tests.ts @@ -14,6 +14,9 @@ test1 = insert(arr1, arr2, undefined); // throw const test2: (number | null)[] = insert(arr1, null, 1); const test3: unknown[] = insert([], []); +const readOnlyArr: readonly unknown[] = [] +insert(readOnlyArr, readOnlyArr) + // Not OK // @ts-expect-error insert(undefined, arr2, 4); From e886ec533d37f38d0bf0dae90e1f456dc1cd8e48 Mon Sep 17 00:00:00 2001 From: Masahiko Shin Date: Tue, 25 Apr 2023 16:59:48 +0900 Subject: [PATCH 03/13] Add readonly array support to array-mean --- packages/array-mean/index.d.ts | 4 ++-- packages/array-mean/index.tests.ts | 4 ++++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/array-mean/index.d.ts b/packages/array-mean/index.d.ts index 8c4c55402..914bf511f 100644 --- a/packages/array-mean/index.d.ts +++ b/packages/array-mean/index.d.ts @@ -2,5 +2,5 @@ * Finds the mean of a set of numbers * @throws {Error} if array is empty */ -export default function mean(arr: number[]): number; -export default function mean(...arr: number[]): number; +export default function mean(arr: readonly number[]): number; +export default function mean(...arr: readonly number[]): number; diff --git a/packages/array-mean/index.tests.ts b/packages/array-mean/index.tests.ts index 1d52bb261..1404092f0 100644 --- a/packages/array-mean/index.tests.ts +++ b/packages/array-mean/index.tests.ts @@ -11,6 +11,10 @@ test1 = mean(4); mean([]) mean() +const readOnlyArr: readonly number[] = [1, 2, 3] +mean(readOnlyArr) +mean(...readOnlyArr) + // Not OK // @ts-expect-error From 4b9497704202b4d3fb3cec61fb5ec8ea80fcdf21 Mon Sep 17 00:00:00 2001 From: Masahiko Shin Date: Tue, 25 Apr 2023 17:03:21 +0900 Subject: [PATCH 04/13] Add readonly array support to array-median --- packages/array-median/index.d.ts | 4 ++-- packages/array-median/index.tests.ts | 3 +++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/packages/array-median/index.d.ts b/packages/array-median/index.d.ts index 8a7903314..af0a7deac 100644 --- a/packages/array-median/index.d.ts +++ b/packages/array-median/index.d.ts @@ -2,5 +2,5 @@ * Finds the median of a set of numbers * @throws {Error} when array.length is 0 */ -export default function median(arr: number[]): number; -export default function median(...arr: number[]): number; +export default function median(arr: readonly number[]): number; +export default function median(...arr: readonly number[]): number; diff --git a/packages/array-median/index.tests.ts b/packages/array-median/index.tests.ts index d4d3bf038..baefdf77f 100644 --- a/packages/array-median/index.tests.ts +++ b/packages/array-median/index.tests.ts @@ -8,6 +8,9 @@ median(1) median(1, 2) median(1, 2, 3) +const readonlyArr: readonly number[] = [1, 2, 3] +median(readonlyArr) + //OK but throws median() median([]) From 897caede13067bebcbbdd9e725b6e191732adc9c Mon Sep 17 00:00:00 2001 From: Masahiko Shin Date: Tue, 25 Apr 2023 17:16:07 +0900 Subject: [PATCH 05/13] Add readonly array support to array-mode --- packages/array-mode/index.d.ts | 4 ++-- packages/array-mode/index.tests.ts | 4 ++++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/array-mode/index.d.ts b/packages/array-mode/index.d.ts index 19ee28694..2d1648d11 100644 --- a/packages/array-mode/index.d.ts +++ b/packages/array-mode/index.d.ts @@ -8,5 +8,5 @@ * mode(1, 2, 1, 2, 3) * // => [1, 2] */ -export default function mode(arr: number[]): number | number[]; -export default function mode(...arr: number[]): number | number[]; +export default function mode(arr: readonly number[]): number | number[]; +export default function mode(...arr: readonly number[]): number | number[]; diff --git a/packages/array-mode/index.tests.ts b/packages/array-mode/index.tests.ts index 96aae43a0..3271d5f1b 100644 --- a/packages/array-mode/index.tests.ts +++ b/packages/array-mode/index.tests.ts @@ -8,6 +8,10 @@ mode(1) mode(1, 2) mode(1, 2, 3) +const readonlyArr: readonly number[] = [1, 2, 2] +mode(readonlyArr) +mode(...readonlyArr) + // Kinda OK //Note: these will cause runtime errors, accepting the tradeoff to keep typings readable mode([]) From 872f162f0c38168cde131e068435c6a7eaf8bc31 Mon Sep 17 00:00:00 2001 From: Masahiko Shin Date: Tue, 25 Apr 2023 17:18:36 +0900 Subject: [PATCH 06/13] Add readonly array support to array-percentile --- packages/array-percentile/index.d.ts | 2 +- packages/array-percentile/index.tests.ts | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/array-percentile/index.d.ts b/packages/array-percentile/index.d.ts index 6ed71a48f..021333e62 100644 --- a/packages/array-percentile/index.d.ts +++ b/packages/array-percentile/index.d.ts @@ -11,4 +11,4 @@ * percentile([1, 2, 3], 1.0); // => 3 * percentile([15, 20, 35, 40, 50], .40); // => 26 */ -export default function percentile(arr: number[], percentage: number): number +export default function percentile(arr: readonly number[], percentage: number): number diff --git a/packages/array-percentile/index.tests.ts b/packages/array-percentile/index.tests.ts index a0a400cf1..89e1d95ee 100644 --- a/packages/array-percentile/index.tests.ts +++ b/packages/array-percentile/index.tests.ts @@ -7,12 +7,13 @@ percentile([1, 2, 3, 4, 5], 0.44) percentile([100, 101, 92, 4, 102, 66, 66], 0.65) percentile([100, 101, 92, 4, 102, 32], 0.50) +const readonlyArr: readonly number[] = [1, 2, 2] +percentile(readonlyArr, 0.5) //Ok but throws errors percentile([], 0.50) percentile([], 1.5) - // @ts-expect-error percentile(1, 2, 3, 4, 5, 0.50); From 7bcd7b902901ed266a6940e7635d1d6bbedaa981 Mon Sep 17 00:00:00 2001 From: Masahiko Shin Date: Tue, 25 Apr 2023 17:21:24 +0900 Subject: [PATCH 07/13] Add readonly array support to array-skewness --- packages/array-skewness/index.d.ts | 4 ++-- packages/array-skewness/index.tests.ts | 4 ++++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/array-skewness/index.d.ts b/packages/array-skewness/index.d.ts index 817bf97ec..25fc4ffb2 100644 --- a/packages/array-skewness/index.d.ts +++ b/packages/array-skewness/index.d.ts @@ -1,3 +1,3 @@ -declare function skewness(arr: number[]): number -declare function skewness(...arr: number[]): number +declare function skewness(arr: readonly number[]): number +declare function skewness(...arr: readonly number[]): number export default skewness diff --git a/packages/array-skewness/index.tests.ts b/packages/array-skewness/index.tests.ts index 181e2863a..f9a14c00f 100644 --- a/packages/array-skewness/index.tests.ts +++ b/packages/array-skewness/index.tests.ts @@ -9,7 +9,11 @@ skewness([1, 2, 3, 4, 9]); skewness([4]); +const readonlyArr: readonly number[] = [1, 2, 3] +skewness(readonlyArr) +skewness(...readonlyArr) +// NG // @ts-expect-error skewness(["3", 2]); // throws // @ts-expect-error From c2573f1f3673fda0b011fe73e0e47661531a1936 Mon Sep 17 00:00:00 2001 From: Masahiko Shin Date: Tue, 25 Apr 2023 17:23:36 +0900 Subject: [PATCH 08/13] Add readonly array support to array-tandard-deviation --- packages/array-standard-deviation/index.d.ts | 4 ++-- packages/array-standard-deviation/index.tests.ts | 4 ++++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/array-standard-deviation/index.d.ts b/packages/array-standard-deviation/index.d.ts index 7df544a24..e47b30205 100644 --- a/packages/array-standard-deviation/index.d.ts +++ b/packages/array-standard-deviation/index.d.ts @@ -1,3 +1,3 @@ -declare function stdev(arr: number[]): number -declare function stdev(...arr: number[]): number +declare function stdev(arr: readonly number[]): number +declare function stdev(...arr: readonly number[]): number export default stdev diff --git a/packages/array-standard-deviation/index.tests.ts b/packages/array-standard-deviation/index.tests.ts index 2e2b329c6..e7fec0af5 100644 --- a/packages/array-standard-deviation/index.tests.ts +++ b/packages/array-standard-deviation/index.tests.ts @@ -6,6 +6,10 @@ standardDeviation([1, 2, 3, 2, 4, 1]); standardDeviation(1, 2, 3, 4, 5, -6); standardDeviation([1, 2, 3, 4, 9]); +const readonlyArr: readonly number[] = [1, 2, 3] +standardDeviation(readonlyArr) +standardDeviation(...readonlyArr) + //OK But throws standardDeviation([4]); standardDeviation([]); From b2f427ffea7657f111fe0f3477f35cfe37fa6f6e Mon Sep 17 00:00:00 2001 From: Masahiko Shin Date: Tue, 25 Apr 2023 17:25:59 +0900 Subject: [PATCH 09/13] Add readonly array support to array-tandard-variance --- packages/array-variance/index.d.ts | 4 ++-- packages/array-variance/index.tests.ts | 5 +++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/packages/array-variance/index.d.ts b/packages/array-variance/index.d.ts index 2be72dba3..bece0ab6d 100644 --- a/packages/array-variance/index.d.ts +++ b/packages/array-variance/index.d.ts @@ -1,3 +1,3 @@ -declare function variance(arr: number[]): number -declare function variance(...arr: number[]): number +declare function variance(arr: readonly number[]): number +declare function variance(...arr: readonly number[]): number export default variance diff --git a/packages/array-variance/index.tests.ts b/packages/array-variance/index.tests.ts index 0d635b6ad..a9815f434 100644 --- a/packages/array-variance/index.tests.ts +++ b/packages/array-variance/index.tests.ts @@ -8,6 +8,11 @@ variance([1, 2, 3, 4, 9]); variance([4]); +const readonlyArr: readonly number[] = [1, 2, 3]; +variance(readonlyArr); +variance(...readonlyArr); + +// NG // @ts-expect-error variance(["3", 2]); // throws // @ts-expect-error From 8209cbdeaf50c7e47923a33d065fe78c2e37238c Mon Sep 17 00:00:00 2001 From: Masahiko Shin Date: Tue, 25 Apr 2023 17:28:44 +0900 Subject: [PATCH 10/13] Add readonly array support to array-permutation --- packages/array-permutations/index.d.ts | 2 +- packages/array-permutations/index.tests.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/array-permutations/index.d.ts b/packages/array-permutations/index.d.ts index de924c122..3a05e788a 100644 --- a/packages/array-permutations/index.d.ts +++ b/packages/array-permutations/index.d.ts @@ -1 +1 @@ -export default function permutations(arr: T[]): T[][]; // for empty arrays +export default function permutations(arr: readonly T[]): T[][]; // for empty arrays diff --git a/packages/array-permutations/index.tests.ts b/packages/array-permutations/index.tests.ts index 79cc22c23..6546f3c04 100644 --- a/packages/array-permutations/index.tests.ts +++ b/packages/array-permutations/index.tests.ts @@ -6,7 +6,7 @@ const test2: number[][] = permutations([1, 2, 3, 4, 5]); const test3: (string | boolean)[][] = permutations(['a', true]) const test4: (string | boolean | number)[][] = permutations([true, 'a', 1, false]) -const numbers: number[] = [] +const numbers: readonly number[] = [] let test5: number[][] = permutations(numbers) // Not OK From d3088b783aa3ea3de6b52862301bba58c70023fc Mon Sep 17 00:00:00 2001 From: Masahiko Shin Date: Tue, 25 Apr 2023 17:45:45 +0900 Subject: [PATCH 11/13] Add readonly array support to array-remove --- packages/array-remove/index.d.ts | 2 +- packages/array-remove/index.tests.ts | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/packages/array-remove/index.d.ts b/packages/array-remove/index.d.ts index 9756f942e..dc3eefaa7 100644 --- a/packages/array-remove/index.d.ts +++ b/packages/array-remove/index.d.ts @@ -1 +1 @@ -export default function remove(arr1: T[], arr2: U[]): (V | Exclude)[]; \ No newline at end of file +export default function remove(arr1: readonly T[], arr2: readonly U[]): (V | Exclude)[]; diff --git a/packages/array-remove/index.tests.ts b/packages/array-remove/index.tests.ts index 50e4ee870..f1ae8e57a 100644 --- a/packages/array-remove/index.tests.ts +++ b/packages/array-remove/index.tests.ts @@ -9,6 +9,9 @@ const test5: (number | true)[] = remove([1, 'a', true, false], [2, 'a', false]) remove([], []) remove([], [1, 2, 3]) +const readonlyArr: readonly number[] = [1, 2, 3] +remove(readonlyArr, readonlyArr) + // Not OK // @ts-expect-error remove() @@ -25,4 +28,4 @@ remove({}) // @ts-expect-error remove("abc", "a") // @ts-expect-error -const test6: string[] = remove([1, 2, 'a'], ['a']) \ No newline at end of file +const test6: string[] = remove([1, 2, 'a'], ['a']) From d0bac2f920090b41d92019f998c03f1c2d7e5b42 Mon Sep 17 00:00:00 2001 From: Masahiko Shin Date: Tue, 25 Apr 2023 18:04:00 +0900 Subject: [PATCH 12/13] Add readonly array support to array-shuffle --- packages/array-shuffle/index.d.ts | 2 +- packages/array-shuffle/index.tests.ts | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/packages/array-shuffle/index.d.ts b/packages/array-shuffle/index.d.ts index b0233a33d..ed319fa60 100644 --- a/packages/array-shuffle/index.d.ts +++ b/packages/array-shuffle/index.d.ts @@ -2,4 +2,4 @@ type Options = { shuffleAll: boolean } -export default function shuffle(arr: T[], options?: Options): T[] \ No newline at end of file +export default function shuffle(arr: readonly T[], options?: Options): T[] diff --git a/packages/array-shuffle/index.tests.ts b/packages/array-shuffle/index.tests.ts index 109afbd41..0911803c1 100644 --- a/packages/array-shuffle/index.tests.ts +++ b/packages/array-shuffle/index.tests.ts @@ -7,6 +7,9 @@ shuffle([true, false, 1]) shuffle([1, 2, 3], { shuffleAll: true }) shuffle([1, 2, 3], { shuffleAll: false }) +const readonlyArr: readonly number[] = [1, 2, 3] +shuffle(readonlyArr) + // Not OK // @ts-expect-error shuffle() @@ -27,4 +30,4 @@ shuffle([], { option: true }) // @ts-expect-error shuffle([1, 2, 3], { shuffleAll: undefined }) // @ts-expect-error -shuffle([], { shuffleAll: 1 }) \ No newline at end of file +shuffle([], { shuffleAll: 1 }) From fef8e076c7cd4bfa0273e1888872ddf1da41af31 Mon Sep 17 00:00:00 2001 From: Masahiko Shin Date: Tue, 25 Apr 2023 18:06:51 +0900 Subject: [PATCH 13/13] Add readonly array support to array-splitAt --- packages/array-split-at/index.d.ts | 2 +- packages/array-split-at/index.tests.ts | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/packages/array-split-at/index.d.ts b/packages/array-split-at/index.d.ts index 041b04f2f..5f5a2ce0c 100644 --- a/packages/array-split-at/index.d.ts +++ b/packages/array-split-at/index.d.ts @@ -1 +1 @@ -export default function splitAt(arr: T[], n?: number | null): [T[], T[]] \ No newline at end of file +export default function splitAt(arr: readonly T[], n?: number | null): [T[], T[]] diff --git a/packages/array-split-at/index.tests.ts b/packages/array-split-at/index.tests.ts index a05d82f41..3f3f5c8cd 100644 --- a/packages/array-split-at/index.tests.ts +++ b/packages/array-split-at/index.tests.ts @@ -6,6 +6,9 @@ const test2: [number[], number[]] = splitAt([1, 2, 3, 4, 5], -1); // [[1, 2, 3, const test3: [unknown[], unknown[]] = splitAt([], 3); // [[], []] const test4: [boolean[], boolean[]] = splitAt([true, false]); // [[], [true, false]] +const readonlyArr: readonly number[] = [1, 2, 3] +splitAt(readonlyArr, 1) + // Not OK // @ts-expect-error splitAt() @@ -18,4 +21,4 @@ splitAt([], true) // @ts-expect-error splitAt([], []) // @ts-expect-error -splitAt([], 2, 3) \ No newline at end of file +splitAt([], 2, 3)