Skip to content

Commit

Permalink
fix partial typing
Browse files Browse the repository at this point in the history
  • Loading branch information
Ray Epps authored and Ray Epps committed Feb 21, 2024
1 parent 3c6c3e8 commit fb1a841
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
23 changes: 19 additions & 4 deletions src/curry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -380,13 +380,28 @@ export function compose(...funcs: ((...args: any[]) => any)[]) {
return funcs.reverse().reduce((acc, fn) => fn(acc))
}

export const partial = <T extends any[], R>(
/**
* This type produces the type array of TItems with all the type items
* in TItemsToRemove removed from the start of the array type.
*
* @example
* ```
* RemoveItemsInFront<[number, number], [number]> = [number]
* RemoveItemsInFront<[File, number, string], [File, number]> = [string]
* ```
*/
type RemoveItemsInFront<
TItems extends any[],
TItemsToRemove extends any[]
> = TItems extends [...TItemsToRemove, ...infer TRest] ? TRest : TItems

export const partial = <T extends any[], TA extends Partial<T>, R>(
fn: (...args: T) => R,
...args: Partial<T>
...args: TA
) => {
return (...rest: T) => fn(...([...args, ...rest] as T))
return (...rest: RemoveItemsInFront<T, TA>) =>
fn(...([...args, ...rest] as T))
}

/**
* Like partial but for unary functions that accept
* a single object argument
Expand Down
3 changes: 2 additions & 1 deletion src/tests/curry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,8 @@ describe('curry module', () => {
test('passes single args', () => {
const add = (a: number, b: number) => a + b
const expected = 20
const result = (_.partial(add, 10) as any)(10)
const partialed = _.partial(add, 10)
const result = partialed(10)
assert.equal(result, expected)
})
test('passes many args', () => {
Expand Down

0 comments on commit fb1a841

Please sign in to comment.