1

I'm trying to remove the first argument from a function that has a generic argument: I would like something like this:

type GenericFunc = <T extends string | number>(c: any, p: T) => T;

type GenericFuncWithoutFirst = OmitFirstArg<GenericFunc> // = <T extends string | number>(p: T) => T;

I've found this related post: Typescript remove first argument from a function

Which suggests:

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

But this seems to remove the generic type inference when calling the function. I've made a demo of this in typescript playground

Any help solving this would be much appreciated.

  • This cannot be done purely at the type level in TypeScript. If you're willing to drop down to the value level you can use [higher order type inference from generic functions](//www.typescriptlang.org/docs/handbook/release-notes/typescript-3-4.html#higher-order-type-inference-from-generic-functions) like [this playground link](https://tsplay.dev/w6PyRm) shows. That may or may not work depending on your use cases. Does this fully address your question? If so I can write up an answer; if not, what am I missing? – jcalz Jul 21 '22 at 20:14
  • I noticed that replacing `(p: P) => R` with `

    (p: P) => P` fixes it, although then you get warnings that `infer P` and `infer R` aren't used. It seems like the issue could be related to the lack of generics in the definition of `RemoveFirstArgument`, but the details aren't clear to me.

    – Ryan Pattillo Jul 21 '22 at 20:29

0 Answers0