Skip to content

Commit

Permalink
fix(partition): Add readonly type to the first argument of `partiti…
Browse files Browse the repository at this point in the history
…on` (#32)

* fix: Add the type readonly array to the partition

* fix: revert the partition documentation

---------

Co-authored-by: Jonghyeon Ko <[email protected]>
  • Loading branch information
ssi02014 and manudeli authored Jun 13, 2024
1 parent d7c37c9 commit 04ec2c2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
13 changes: 12 additions & 1 deletion src/array/partition.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, expect, it } from 'vitest';
import { describe, expect, it, expectTypeOf } from 'vitest';
import { partition } from './partition';

describe('partition', () => {
Expand All @@ -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>>();
});
});
2 changes: 1 addition & 1 deletion src/array/partition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[] = [];

Expand Down

0 comments on commit 04ec2c2

Please sign in to comment.