1

I'm trying to remove the first argument from a function with multiple overloads.

I've used the type below from this Stack Overflow answer to do this for functions without overloads but am having some trouble to get it to work with overloads.

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

Example:

function func1(arg1: any, arg2: string): string;
function func1(arg1: any, arg2: number): number;
function func1(arg1: any, arg2: string | number): string | number {
    ...
}

type Func2 = OmitFirstArg<typeof func1>;

In this example type Func2 = (arg2: number) => number.

I need it to be:

type Func2 = {
    (arg2: string): string;
    (arg2: number): number;
}
Niekvb
  • 144
  • 1
  • 8
  • 1
    Hmm, I'm not sure this is possible at the moment. At least not easily. See: https://github.com/microsoft/TypeScript/issues/32164 There are some complex and somewhat limited work arounds: https://github.com/microsoft/TypeScript/issues/32164#issuecomment-764660652 – Alex Wayne Jul 19 '21 at 20:31
  • 1
    Overloads cannot easily be manipulated programmatically; see [this q/a](https://stackoverflow.com/questions/59535995/parameters-generic-of-overloaded-function-doesnt-contain-all-options) for more info. You can do [this](https://tsplay.dev/NVKgqm), but it's yucky and only works for a small number of overloads. I'm not sure if this should be its own answer, or if I should close it as a duplicate of other such overloads-are-hard-to-manipulate questions. – jcalz Jul 19 '21 at 20:35
  • I'm not sure if there's a nice and clean way to do this either at this moment. But for now I should be able to get this to work with @jcalz answer. Thanks! – Niekvb Jul 19 '21 at 21:11

0 Answers0