0

I'm trying to get the Parameters from an overloaded function

interface FuncContainer {
    someFunc(name: string, value: number): number;
}

Here, I have an interface that has a function someFunc. What I'm really interested in is the type of value - this is pretty simple:

type Value = Parameters<FuncContainer["someFunc"]>[1]; // Value is type number

The problem is when using overloads, the last overload is always used by Parameters (via infer):

interface FuncContainer {
    someFunc(name: string, value: number): number;
    someFunc(name: string): number;
}

type Args = Parameters<FuncContainer["someFunc"]>;    // [str: string]
type Value = Parameters<FuncContainer["someFunc"]>[1]; // undefined

I've tried leveraging extends with no effect:

type Parameter2<F extends (name: string, value: any) => any> = Parameters<F>[1];
type Value = Parameter2<FuncContainer["someFunc"]>; // undefined

Is there any way to specify which overload (by index, by type, whatever) is used when using Parameters?

4
  • As I think the last overload is implementation. That's why it takes the last. Overloads should be ordered from most specific to least specific. From least specific overload (implementation) you can retrieve any parameter type you want. Commented Oct 23, 2020 at 14:44
  • @НиколайГольцев The implementation of a function (the lowest) must have the widest type def, and all of the overloads must extend it. I do not believe that there are any other ordering rules in overloads. Also: my overloads, here, are just in an interface - there is no implementation. The idea that it's picking the least specific overload was interesting - I tested it out by flipping the two versions of someFunc but it still picked the lower of the two (which made type Value = ... equal to number. Commented Oct 23, 2020 at 14:55
  • The implementation of a function is not part of its call signatures, so @НиколайГольцев's comment doesn't quite apply. It is true that the ordering of call signatures should be from most specific to least specific, but that's just to help the compiler pick the right one when calling it. But the least specific call signature is not the same as the implementation signature. Commented Oct 23, 2020 at 15:03
  • This question is a duplicate of this one too but I can't mark it as such because it doesn't have an upvoted answer Commented Oct 23, 2020 at 15:07

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.