Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parameters<> helper only shows the last declared overload #54223

Closed
m-gallesio opened this issue May 12, 2023 · 3 comments
Closed

Parameters<> helper only shows the last declared overload #54223

m-gallesio opened this issue May 12, 2023 · 3 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@m-gallesio
Copy link

m-gallesio commented May 12, 2023

Bug Report

πŸ”Ž Search Terms

Parameters, Overload

πŸ•— Version & Regression Information

5.0.4

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

interface X {
    a(x: string): void;
    a(x: number): void;
}

type T = Parameters<X["a"]>[0];

let n: T = 1; // works
let s: T = "a"; // ERROR: Type 'string' is not assignable to type 'number'

πŸ™ Actual behavior

When using the Parameters<T> helper type on a function with overloads, the resulting type considers only the very last overload.

I noticed this while trying to extract tpe type of the second argument of String.replace which can be both a string and a replacer function; only the replacer function is seen.

πŸ™‚ Expected behavior

I would expect the previous code to consider valid all types which can be used for the function's parameters.
In the given example T should be resolved as string | number.

Possibly related to #53541

@MartinJohns
Copy link
Contributor

MartinJohns commented May 12, 2023

This is a design limitation and working as intended. Parameters<> is a conditional type using type inference, and these have trouble with overloads. See Inferring Within Conditional Types:

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.

@RyanCavanaugh RyanCavanaugh added the Working as Intended The behavior described is the intended behavior; this is not a bug label May 12, 2023
@m-gallesio
Copy link
Author

Understood, thank you for your quick response.

@microsoft-github-policy-service

This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

3 participants