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:
Gromit (전민재) 2024-06-13 20:30:08 +09:00 committed by GitHub
parent d7c37c9fb6
commit 04ec2c2fd1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 13 additions and 2 deletions

View File

@ -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>>();
});
});

View File

@ -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[] = [];