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

Merging signatures using a union as rest parameters results in never. #47754

Closed
jespertheend opened this issue Feb 6, 2022 · 3 comments Β· Fixed by #47934
Closed

Merging signatures using a union as rest parameters results in never. #47754

jespertheend opened this issue Feb 6, 2022 · 3 comments Β· Fixed by #47934
Assignees
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue

Comments

@jespertheend
Copy link
Contributor

Bug Report

πŸ”Ž Search Terms

merged parameters rest union never

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about union

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

type RestParams = [y: string] | [y: number];

type Signature = (x: string, ...rest: RestParams) => void;

type MergedParams = Parameters<Signature>; // never

πŸ™ Actual behavior

MergedParams is of type never.

πŸ™‚ Expected behavior

MergedParams is of type [x: string, y: string] | [x: string, y: number]

I realise this can sort of be done by using [y: string | number] as type for RestParams. However, I'm using unions in parameters like this in order to overload the signatures of functions in jsdoc as shown here: #25590 (comment)
If I use a union like that it will show up as union in VSCode, instead of using the multiple signatures UI.
Here's an example of this: Playground link.

@RyanCavanaugh
Copy link
Member

@ahejlsberg what's the intended behavior here?

@RyanCavanaugh RyanCavanaugh added the Needs Investigation This issue needs a team member to investigate its status. label Feb 10, 2022
@ahejlsberg
Copy link
Member

The core issue here is that we don't always consider equivalent signatures to be assignable. For example:

declare let f1: (...rest: [string, string] | [string, number]) => void;
declare let f2: (x: string, ...rest: [string] | [number]) => void;

f1 = f2;  // Error
f2 = f1;  // Error

Ideally the assignments above would be permitted as the signatures are functionally equivalent.

@ahejlsberg ahejlsberg added Bug A bug in TypeScript and removed Needs Investigation This issue needs a team member to investigate its status. labels Feb 16, 2022
@ahejlsberg
Copy link
Member

ahejlsberg commented Feb 16, 2022

I should add that a workaround for the reported issue is to write the signature such that all parameters are specified in the rest tuple:

type RestParams = [x: string, y: string] | [x: string y: number];

type Signature = (...rest: RestParams) => void;

type MergedParams = Parameters<Signature>; // [x: string, y: string] | [x: string y: number]

But ideally that wouldn't be necessary.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants