Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add readonly array support to some packages. #549

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
2 changes: 1 addition & 1 deletion packages/array-compact/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ type Falsy = false | null | undefined | '' | 0 | 0n;
* compact([1, 2, [], 4, {}]);
* // => [1, 2, [], 4, {}]
*/
export default function compact<T>(arr: (Falsy | T)[]): T[];
export default function compact<T>(arr: readonly (Falsy | T)[]): T[];
3 changes: 3 additions & 0 deletions packages/array-compact/index.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
6 changes: 5 additions & 1 deletion packages/array-insert/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@
* insert([1, 2, 5, 6], ['a', 'c', 'e'], 2);
* // => [1, 2, 'a', 'c', 'e', 5, 6]
*/
export default function insert<T, U>(arr: T[], val: U[] | U, index?: number): (T | U)[]
export default function insert<T, U>(
arr: readonly T[],
val: readonly U[] | U,
index?: number
): (T | U)[]
3 changes: 3 additions & 0 deletions packages/array-insert/index.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions packages/array-mean/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
4 changes: 4 additions & 0 deletions packages/array-mean/index.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions packages/array-median/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
3 changes: 3 additions & 0 deletions packages/array-median/index.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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([])
Expand Down
4 changes: 2 additions & 2 deletions packages/array-mode/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[];
4 changes: 4 additions & 0 deletions packages/array-mode/index.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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([])
Expand Down
2 changes: 1 addition & 1 deletion packages/array-percentile/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 2 additions & 1 deletion packages/array-percentile/index.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion packages/array-permutations/index.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export default function permutations<T>(arr: T[]): T[][]; // for empty arrays
export default function permutations<T>(arr: readonly T[]): T[][]; // for empty arrays
2 changes: 1 addition & 1 deletion packages/array-permutations/index.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion packages/array-remove/index.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export default function remove<T, U, V extends T>(arr1: T[], arr2: U[]): (V | Exclude<T, U>)[];
export default function remove<T, U, V extends T>(arr1: readonly T[], arr2: readonly U[]): (V | Exclude<T, U>)[];
5 changes: 4 additions & 1 deletion packages/array-remove/index.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -25,4 +28,4 @@ remove({})
// @ts-expect-error
remove("abc", "a")
// @ts-expect-error
const test6: string[] = remove([1, 2, 'a'], ['a'])
const test6: string[] = remove([1, 2, 'a'], ['a'])
2 changes: 1 addition & 1 deletion packages/array-shuffle/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ type Options = {
shuffleAll: boolean
}

export default function shuffle<T>(arr: T[], options?: Options): T[]
export default function shuffle<T>(arr: readonly T[], options?: Options): T[]
5 changes: 4 additions & 1 deletion packages/array-shuffle/index.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -27,4 +30,4 @@ shuffle([], { option: true })
// @ts-expect-error
shuffle([1, 2, 3], { shuffleAll: undefined })
// @ts-expect-error
shuffle([], { shuffleAll: 1 })
shuffle([], { shuffleAll: 1 })
4 changes: 2 additions & 2 deletions packages/array-skewness/index.d.ts
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions packages/array-skewness/index.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion packages/array-split-at/index.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export default function splitAt<T>(arr: T[], n?: number | null): [T[], T[]]
export default function splitAt<T>(arr: readonly T[], n?: number | null): [T[], T[]]
5 changes: 4 additions & 1 deletion packages/array-split-at/index.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -18,4 +21,4 @@ splitAt([], true)
// @ts-expect-error
splitAt([], [])
// @ts-expect-error
splitAt([], 2, 3)
splitAt([], 2, 3)
4 changes: 2 additions & 2 deletions packages/array-standard-deviation/index.d.ts
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions packages/array-standard-deviation/index.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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([]);
Expand Down
4 changes: 2 additions & 2 deletions packages/array-variance/index.d.ts
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions packages/array-variance/index.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down