Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript remove first argument from a function

I have a possibly weird situation that I'm trying to model with typescript.

I have a bunch of functions with the following format

type State = { something: any }


type InitialFn = (state: State, ...args: string[]) => void

I would like to be able to create a type that represents InitialFn with the first argument removed. Something like

// this doesn't work, as F is unused, and args doesn't correspond to the previous arguments
type PostTransformationFn<F extends InitialFn> = (...args: string[]) => void

Is this possible?

like image 254
MDalt Avatar asked Nov 08 '19 10:11

MDalt


1 Answers

I think you can do that in a more generic way:

type OmitFirstArg<F> = F extends (x: any, ...args: infer P) => infer R ? (...args: P) => R : never;

and then:

type PostTransformationFn<F extends InitialFn> = OmitFirstArg<F>;

PG

like image 101
georg Avatar answered Oct 10 '22 05:10

georg