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() 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); 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 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([]) 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([]) 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); 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 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']) 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 }) 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 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) 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([]); 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