1

I'm trying to get an array of type parameters of a function with all the declarations (Overloads) with or concatenation.

For example, I can use the Parameters<T> but this function only get the last declaration.

interface FOO {
    foo(a: string, b: string): string;
    foo(a: boolean, b: string): string;
    foo(a: boolean, b:string, c: number): string;
};

type params = Parameters<FOO["foo"]>; // [boolean, string, number]

Actually, the params type is [boolean, string, number] but I expect the type as [string, string] | [boolean, string] | [boolean, string, number].

jtwalters
  • 1,024
  • 7
  • 25

1 Answers1

2

TS doesn't support infer union param type from overloaded function. The way TS function overload works is like switch-case, it eventually collapses to one of the signatures. Check this section:

When inferring from a type with multiple call signatures (such as the type of an overloaded function), inferences are made from the last signature (which, presumably, is the most permissive catch-all case). It is not possible to perform overload resolution based on a list of argument types.

If such params type is important to you, you could add:

interface FOO {
    foo(a: string, b: string): string;
    foo(a: boolean, b: string): string;
    foo(a: boolean, b: string, c: number): string;
    foo(...args: [string, string] | [boolean, string] | [boolean, string, number]): string;
};

Edit: OK, I was wrong. turns out Titian the TS wizard always finds a way, even when it's officially claimed "not possible". Be sure to checkout his brilliant answer.

hackape
  • 18,643
  • 2
  • 29
  • 57