0

Is there a way to remove one of the elements from a type that is an array? I'm looking to curry something and make the whole thing easier.

E.g.

function NEW_PERSON (name : string, age : number, weight : number, height : number) {

}

function CURRIED_NEW_PERSON (name : string) {
    return function (...args : Parameters<typeof NEW_PERSON>) { // remove index 0
        return NEW_PERSON(name, ...args); // error due to 5 arguments instead of 4
    }
}

How do I remove index 0 from Parameters<typeof NEW_PERSON>?

A. L
  • 11,695
  • 23
  • 85
  • 163

1 Answers1

0

We can create a utility type which extracts every element except the first one from a tuple using infer.

type ExceptFirst<T extends any[]> = 
  Parameters<typeof NEW_PERSON> extends [any, ...infer REST] 
    ? REST 
    : never

function CURRIED_NEW_PERSON (name : string) {
    return function (...args : ExceptFirst<Parameters<typeof NEW_PERSON>>) {
        return NEW_PERSON(name, ...args);
    }
}

Playground

Tobias S.
  • 21,159
  • 4
  • 27
  • 45
  • I was wondering if this is what the OP wanted, because [this answer](https://stackoverflow.com/a/67605309/13258211) describes it in detail. –  Nov 29 '22 at 09:05