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

Rest declarations in destructured arrays receiving incorrect types from tuples declaring the types in the destructured array #16700

Closed
zajrik opened this issue Jun 22, 2017 · 4 comments
Labels
Fixed A PR has been merged for this issue

Comments

@zajrik
Copy link

zajrik commented Jun 22, 2017

TypeScript Version: 2.3.4

Code

public async action([res, commandName, ...roleNames]: [ResourceLoader, string, string[]]): Promise<void> { ... }

Expected behavior:
When destructuring the method parameter (which can be an array of potentially anything after middleware has had its say, but I know exactly what it will be in this case) I've gathered the remaining array values (which I know to be strings) into roleNames and declared the type of the variables in the destructured array via the tuple [ResourceLoader, string, string[]].

I expect roleNames to take on the declared type from the tuple as res and commandName have correctly done.

Actual behavior:
When hovering over the roleNames declaration I get the following:

var roleNames: (string | ResourceLoader | string[])[]

Apologies for the convoluted title. Couldn't think of a less verbose description.

@mhegazy mhegazy added the Needs Investigation This issue needs a team member to investigate its status. label Aug 29, 2017
@DanielRosenwasser
Copy link
Member

The type [ResourceLoader, string, string[]] probably isn't what you had in mind. What you really want is an open-ended tuple where all of the trailing elements are known to be strings.

Generally related is #5453.

@zajrik
Copy link
Author

zajrik commented Mar 23, 2018

I've actually recently re-engineered the code in question because it allowed me to merge the functionality of two separate things into one, so I'm actually just receiving a string where ...roleNames used to exist and parsing potentially multiple roles within the function body, but I am still interested in a resolution to this problem.

I'm not entirely sure what you mean by open-ended tuple. Given what you've linked is a proposal, is this something that is possible in current TS?

@ahejlsberg
Copy link
Member

Now implemented in #24897.

@mhegazy mhegazy added Fixed A PR has been merged for this issue and removed Needs Investigation This issue needs a team member to investigate its status. labels Jun 26, 2018
@mhegazy mhegazy added this to the TypeScript 3.0 milestone Jun 26, 2018
@zajrik
Copy link
Author

zajrik commented Jul 22, 2018

I somehow missed seeing this get closed, but I saw this change in the 3.0 RC and thought "Hey, that was my issue!"

Glad to see this resolved. Cheers 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

4 participants