Skip to content

Commit

Permalink
Add readonly array support for some packages (#496)
Browse files Browse the repository at this point in the history
* improve type definition of array-group-by

* improve type definition of array-flatten

* improve type definition of array-index
  • Loading branch information
Masa-Shin authored Sep 10, 2022
1 parent aa9d21e commit c09fe15
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 14 deletions.
4 changes: 2 additions & 2 deletions packages/array-flatten/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
type RecursiveList<T> = (T | T[] | RecursiveList<T>)[];
type RecursiveList<T> = readonly (T | readonly T[] | RecursiveList<T>)[];

/**
* Flattens an array
Expand All @@ -11,5 +11,5 @@ type RecursiveList<T> = (T | T[] | RecursiveList<T>)[];
* flatten([[1, [2, 3]], [[4, 5], 6, 7, [8, 9]]], 1);
* // => [1, [2, 3], [[4, 5], 6, 7, [8, 9]]]
*/
declare function flatten<T>(arr: RecursiveList<T>, depth? : number): T[];
declare function flatten<T>(arr: RecursiveList<T>, depth?: number): T[];
export default flatten;
11 changes: 6 additions & 5 deletions packages/array-flatten/index.tests.ts
Original file line number Diff line number Diff line change
@@ -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);

Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion packages/array-group-by/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
* @param resolver function used to resolve group key
*/
export default function groupBy<T, G extends keyof any = keyof any>(
arr: T[],
arr: readonly T[],
resolver: (arg: T) => G
): Record<G, T[]>;
3 changes: 2 additions & 1 deletion packages/array-group-by/index.tests.ts
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
2 changes: 1 addition & 1 deletion packages/array-index/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@
* index([{id: 0, val: 0}, {id: 0, val: 1}], 'id');
* // => {first: {id: 0, val: 1}}
*/
declare function index<T>(arr: Array<T|null|undefined>, key: string): Record<string, T>;
declare function index<T>(arr: readonly (T | null | undefined)[], key: string): Record<string, T>;
export default index;
9 changes: 5 additions & 4 deletions packages/array-index/index.tests.ts
Original file line number Diff line number Diff line change
@@ -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<string, number>[] = [{ 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');

0 comments on commit c09fe15

Please sign in to comment.