Skip to content

Commit 238631f

Browse files
committed
Improve typings for splitBy
1 parent 7cbc491 commit 238631f

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

src/helpers.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,15 @@ import { t } from './lang';
88
* @param objects The array to split.
99
* @param key The key of T.
1010
*/
11-
export function splitBy<T>(objects: T[], key: keyof T): T[][] {
12-
const map = {} as Record<string | number, T[]>;
11+
export function splitBy<
12+
T extends Record<string, unknown>,
13+
K extends keyof T,
14+
U extends Record<K, string | number>
15+
>(objects: U[], key: K): U[][] {
16+
const map = {} as Record<string | number, U[]>;
1317

1418
for (const obj of objects) {
15-
const commonValue = obj[key] as string | number;
19+
const commonValue = obj[key];
1620

1721
if (!map[commonValue])
1822
map[commonValue] = [];

0 commit comments

Comments
 (0)