Skip to content

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

Closed

Description

@zajrik

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.

Activity

  1. DanielRosenwasser commented on Mar 22, 2018

    @DanielRosenwasser
    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.

  2. zajrik commented on Mar 23, 2018

    @zajrik
    Author

    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?

  3. ahejlsberg commented on Jun 26, 2018

    @ahejlsberg
    Member

    Now implemented in #24897.

  4. added
    FixedA PR has been merged for this issue
    and removed
    Needs InvestigationThis issue needs a team member to investigate its status.
    on Jun 26, 2018
  5. zajrik commented on Jul 22, 2018

    @zajrik
    Author

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    FixedA PR has been merged for this issue

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions