mirror of
https://github.com/toss/es-toolkit.git
synced 2025-01-06 07:57:12 +03:00
fix(partition): Add readonly
type to the first argument of partition
(#32)
* fix: Add the type readonly array to the partition * fix: revert the partition documentation --------- Co-authored-by: Jonghyeon Ko <jonghyeon@toss.im>
This commit is contained in:
parent
d7c37c9fb6
commit
04ec2c2fd1
@ -1,4 +1,4 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { describe, expect, it, expectTypeOf } from 'vitest';
|
||||
import { partition } from './partition';
|
||||
|
||||
describe('partition', () => {
|
||||
@ -25,4 +25,15 @@ describe('partition', () => {
|
||||
],
|
||||
]);
|
||||
});
|
||||
|
||||
it('should correctly infer the type of a narrow array', () => {
|
||||
const arr = [1, 2, 3, 4, 5] as const;
|
||||
const [evens, odds] = partition(arr, num => num % 2 === 0);
|
||||
|
||||
expect(evens).toEqual([2, 4]);
|
||||
expect(odds).toEqual([1, 3, 5]);
|
||||
|
||||
expectTypeOf(evens).toEqualTypeOf<Array<1 | 2 | 3 | 4 | 5>>();
|
||||
expectTypeOf(odds).toEqualTypeOf<Array<1 | 2 | 3 | 4 | 5>>();
|
||||
});
|
||||
});
|
||||
|
@ -19,7 +19,7 @@
|
||||
* const [even, odd] = partition(array, isEven);
|
||||
* // even will be [2, 4], and odd will be [1, 3, 5]
|
||||
*/
|
||||
export function partition<T>(arr: T[], isInTruthy: (value: T) => boolean): [truthy: T[], falsy: T[]] {
|
||||
export function partition<T>(arr: readonly T[], isInTruthy: (value: T) => boolean): [truthy: T[], falsy: T[]] {
|
||||
const truthy: T[] = [];
|
||||
const falsy: T[] = [];
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user