0

Let's say a simple overloaded function defined as the following:

function f(p: number) : string;
function f(p: string) : undefined;

function f(p: number | string): number | string {
    return (typeof p === 'number') ? String(p) : undefined;
}

Parameters<typeof f>[0] gives me only string. Because it's the last overload defined.

How to choose which overload function to get the parameters from ?
Without having to reorder the definitions of course.

2
  • Do's and Don'ts States to use Union types instead of duplicate overloads differing by param types Commented Jan 31, 2020 at 13:30
  • @AndrewNolan It also states Note that we didn’t make b optional here because the return types of the signatures differ. Like so, since mines have differents returns types, this is not a problem at all. Commented Jan 31, 2020 at 13:37

1 Answer 1

0

Maybe rather use a generic function?

function f<K extends number | string>(p: K): K {
    return p;
}
Sign up to request clarification or add additional context in comments.

1 Comment

No. The snippet I used was just to simplify the exemple. I updated

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.