0

I have the following combinator that converts a mutli-argument function in one that can be partially applied:

type Tuple = any[];

const partial = <A extends Tuple, B extends Tuple, C>
  (f: (...args: (A & B)[]) => C, ...args1: A) => (...args2: B) =>
//     ^^^^^^^^^^^^^^^^^^
    f(...args1, ...args2);

const sum = (v: number, w: number, x: number, y: number, z: number) =>
  w + w + x + y + z;

partial(sum, 1, 2, 3)(4, 5);
//      ^^^

Playground

This doesn't work, because the function argument f must accept various numbers of arguments without using rest syntax. Is there a way to type f?

  • 1
    You can't concatenate tuple types by intersecting them. For example, `[string, number] & [boolean]` is not equivalent to `[string, number, boolean]`. Instead it's an impossible tuple whose `length` is the uninhabitable type `1 & 2`, and whose first element is the uninhabitable type `string & boolean`. There's no built-in tuple concatenation at the type level (see [microsoft/TypeScript#5453](https://github.com/microsoft/TypeScript/issues/5453)) and the workarounds come in various flavors of ugly and unsupported. – jcalz May 01 '20 at 16:24
  • I figured that.. I guess from a type level perspective rest syntax is mainly useful if it yields an array `A[]` instead of a tuple. –  May 01 '20 at 16:39

1 Answers1

0

You can't concatenate tuple types by intersecting them. For example, [string, number] & [boolean] is not equivalent to [string, number, boolean]. Instead it's an impossible tuple whose length is the uninhabitable type 1 & 2, and whose first element is the uninhabitable type string & boolean. There's no built-in tuple concatenation at the type level (see microsoft/TypeScript#5453) and the workarounds come in various flavors of ugly and unsupported.

Here's a workaround that's somewhat ugly and possibly unsupported (although see microsoft/TypeScript#32131 which will introduce new typings for Array.flat() that do pretty much the same thing):

type Prev = [never, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
type Tail<T extends any[]> = 
  ((...t: T) => void) extends ((h: any, ...t: infer R) => void) ? R : never;
type Drop<T extends any[], N extends number> = 
  { 0: T, 1: Drop<Tail<T>, Prev[N]> }[N extends 0 ? 0 : 1];

const partial = <
    X extends any[],
    Y extends Extract<{ [K in keyof Y]: K extends keyof X ? X[K] : never }, any[]>,
    R>(
        f: (...args: X) => R,
        ...args1: Y
    ) => (...args2: Drop<X, Y['length']>): R => f(...[...args1, ...args2] as any);

The type Prev is just a tuple that lets you get from one number to the previous number, up to whatever limit you want. So Prev[4] is 3 and Prev[3] is 2.

The type Tail<T> takes a tuple type T and strips off the first element, leaving everything after. So Tail<[1, 2, 3, 4]> is [2, 3, 4].

The type Drop<T, N> is the possibly-unsupported recursive thing that takes a tuple type T and a number N and strips off the first N elements, leaving everything after. So Drop<T, 1> is basically just Tail<T>, and Drop<[1, 2, 3, 4, 5], 2> is [3, 4, 5].

Finally, the partial() signature is generic in tuple type X, corresponding to the full set of arguments for f, and a tuple type Y, corresponding to the rest of the arguments to partial(), and Y must be some initial segment of X. So if x is [1,2,3,4,5], then Y can be [1], or [1, 2], ... or [1, 2, 3, 4, 5]. And the type R is the return type of f. Then, it returns a new function whose return type is R, and whose argument type is Drop<X, Y['length']>. That is, the returned function accepts the arguments to f after the ones in Y.


Let's see if it works:

const sum = (v: number, w: number, x: number, y: number, z: number) => v + w + x + y + z;

const okay = partial(sum, 1, 2, 3); // const okay: (y: number, z: number) => number
console.log(okay(4, 5)) // 15

const bad = partial(sum, "a", "b", "c"); // error "a" is not number
const alsoBad = partial(sum, 1, 2, 3, 4, 5, 6); // error 6 is not never

Looks good to me.


Okay, hope that helps; good luck!

Playground link to code

jcalz
  • 264,269
  • 27
  • 359
  • 360