Skip to content

Commit

Permalink
add types for just-curry-it (#446)
Browse files Browse the repository at this point in the history
  • Loading branch information
breggle authored Apr 16, 2022
1 parent 0fab200 commit 97616ce
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 6 deletions.
21 changes: 16 additions & 5 deletions packages/function-curry/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
export default curry;

// TODO: get more specific with permssible param types for returned function
declare function curry(
fn: Function,
arity?: number,
) : Function;
type MakeTuple<LEN extends number, T extends any = any, C extends T[] = []> = C['length'] extends LEN ? C : MakeTuple<LEN, T, [T, ...C]>
type CurryOverload<F extends (...args: any[]) => any, N extends unknown[], P extends unknown[]> =
N extends [infer L, ...infer R]
? ((...args: [...P, L]) => CurryInternal<F, R>) & CurryOverload<F, R, [...P, L]>
: () => CurryInternal<F, P>
type CurryInternal<F extends (...args: any[]) => any, N extends unknown[]> =
0 extends N['length'] ? ReturnType<F> : CurryOverload<F, N, []>
type Curry<F extends (...args: any[]) => any, LEN extends number | undefined = undefined, I extends any = any> =
0 extends (LEN extends undefined ? Parameters<F>['length'] : LEN)
? () => ReturnType<F>
: CurryInternal<F, LEN extends undefined ? Parameters<F> : MakeTuple<LEN extends undefined ? 0 : LEN, I>>

declare function curry<F extends (...args: any[]) => any, L extends number>(
fn: F,
arity?: L | undefined,
) : Curry<F, number extends L ? undefined : L, Parameters<F>[number]>;
42 changes: 41 additions & 1 deletion packages/function-curry/index.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,50 @@ curry(add)(1)(2)(3);

curry(add, 1);
curry(add, 1)(1);

function many(a: string, b: number, c: boolean, d: bigint, e: number[], f: string | undefined, g: Record<any, any>): boolean {
return true
}
const return1 = curry(many)('')(123)(false)(BigInt(2))([1])(undefined)({})
const return2 = curry(many)('', 123)(false, BigInt(2))([1], '123')({})
const return3 = curry(many)('', 123, false)(BigInt(2), [1], undefined)({})
const return4 = curry(many)('', 123, false, BigInt(2))([1], undefined, {})
const return5 = curry(many)('', 123, false, BigInt(2), [1])(undefined, {})
const return6 = curry(many)('', 123, false, BigInt(2), [1], undefined)({})
const return7 = curry(many)('', 123, false)(BigInt(2), [1])(undefined)({})
const returns: boolean[] = [return1, return2, return3, return4, return5, return6, return7]

function dynamic(...args: string[]): number {
return args.length
}
const dy1 = curry(dynamic, 1)('')
const dy2 = curry(dynamic, 2)('')('')
const dy3 = curry(dynamic, 3)('')('')('')
const dys: number[] = [dy1, dy2, dy3]

// not OK
// @ts-expect-error
curry(add, 1)(1)(2);
// @ts-expect-error
curry(add, 1)(1)(2)(3);
// @ts-expect-error
curry(add, 4)('');

// @ts-expect-error
curry(many)(123)
// @ts-expect-error
curry(many)('', 123)(123)
// @ts-expect-error
curry(many)('', 123, true)('')
// @ts-expect-error
curry(many)('', 123, true, BigInt(2))('')
// @ts-expect-error
curry(many)('', 123, true, BigInt(2), [1])(123)
// @ts-expect-error
curry(many)('', 123, true, BigInt(2), [1,1], '')('')
// @ts-expect-error
curry(dynamic)(123)

// not OK
// @ts-expect-error
curry();
// @ts-expect-error
Expand Down

0 comments on commit 97616ce

Please sign in to comment.