GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Destructuring array, ability to explicitly tell the variable is unused · Issue #31388 · microsoft/TypeScript

Destructuring array, ability to explicitly tell the variable is unused #31388

New issue
Copy link
New issue
Copy link
Closed
#41378
Closed
Destructuring array, ability to explicitly tell the variable is unused#31388
#41378
Copy link
Labels
CommittedThe team has roadmapped this issueThe team has roadmapped this issueFix AvailableA PR has been opened for this issueA PR has been opened for this issueFixedA PR has been merged for this issueA PR has been merged for this issueSuggestionAn idea for TypeScriptAn idea for TypeScript
Milestone
TypeScript 4.2.0

Description

@jerrygreen
Jerry Green (jerrygreen)
opened on May 14, 2019
Issue body actions

Search Terms

  • declared but never used noUnusedLocals

Suggestion

When you're destructuring an array, I want some variables/const to be declared but explicitly make them unused. Typescript has a feature of underscoring the unused parameters for functions:

const f = (a, _b) => a

But it doesn't work for array destruction:

const [a, _b] = [1, 2]

The motivation of the current behavior is that you can destruct array like this:

const [, , , d] = [1, 2, 3, 4]

But you can't do this (this is invalid syntax):

const f = (, , , d) => d

But whatever syntax is valid, what I say is that the ability to explicitly say a variable is unused, with leading underscore - it's good practice, it's convenient, and it should be supported when destructuring arrays, not in functions only.

P.S. Originally I've described the issue here - #31357, but it's wrongly described as a "bug request", so here's a feature request

Use Cases

Today you make some changes to the code that don't require such a variable, so you don't construct it from the array. You had a perfectly named variable, but you remove it. Tomorrow's programmer needs that variable, but he gives it a different name. Of course, there's code review, etc., so eventually it will get a proper name again - it just adds additional communication which is not needed, consumes time which could be used to something more useful.

Additionally, this syntax looks very weird (don't you think?):

const [, , , d] = [1, 2, 3, 4]

Instead, it's much better to have this:

const [_a, _b, _c, d] = [1, 2, 3, 4]

Examples

Having this code:

const [a, _b] = [1, 2]
const [_a, b] = [1, 2]
export { a, b }

To receive no errors from typescript when noUnusedLocals is set to true.

  • Here's a typescript playground
  • A gist for that:
    git clone [email protected]:b08abed91888a739e4c9be6640f6b625.git test-ts-unused
    cd test-ts-unused
    tsc
    

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.
Reacted by Ian MacLeod, Brandon Bloom, Oleksii Makodzeba, Jerry Green, Thomas Hoorn..., Eric Sundquist, Michael Boselowitz, Zane Hitchcox, Cina Saffary, Michael K and 18 more
Reacted by Giovanni Matina, Oleksandr Tarasiuk and CAO THAI DUONG

Activity

  1. Ryan Cavanaugh (RyanCavanaugh)
    added
    Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureThis means we'd like to hear from more people who would be helped by this feature
    SuggestionAn idea for TypeScriptAn idea for TypeScript
    on May 14, 2019
  2. nevir commented on May 14, 2019

    @nevir
    Ian MacLeod (nevir)
    on May 14, 2019
    More actions

    Additional rationale to support this:

    • More than a few bare commas becomes very tricky to (visually) follow, and is error-prone (particularly for homogenous arrays).

    • These help document the structure of the array; and is especially helpful when interoperating with less-than-perfectly-typed TS or JS code coming from an external source.

    Reacted by Eric Sundquist, Thiago Camargo, Will and SlurpTheo
  3. zwhitchcox commented on Dec 7, 2019

    @zwhitchcox
    Zane Hitchcox (zwhitchcox)
    on Dec 7, 2019
    More actions

    I agree with this and think all underscore prefixed variables should be ignored by the compiler.

    This is very confusing for people coming from Go or Rust who are used to being able to ignore any unused variable, not just destructured parameter variables, and it's a much better DX.

    I personally just spent about an hour trying to figure out why this wasn't working.

    This is what I'm having to do to destructure an array:

    const [Input, /* state */ , /* actions */, meta] = input

    because I want to show what those variables are, even if I don't want to use them. If I want to use them later, it's easier to remember what they are.

    It would be much cleaner to simply have

    const [Input, _state , _actions, meta] = input

    and if I decided I wanted to use them, I just have to remove the underscore (and vice versa).

    Reacted by Jerry Green, Alexander Kuzmin, Jean Paul Cornejo, Philipp Bisson, Thiago Camargo, Will, Toni Villena and SlurpTheo
  4. Jerry Green (jerrygreen)
    changed the title [-]Destructing array, ability to explicitly tell the variable is unused[/-] [+]Destructuring array, ability to explicitly tell the variable is unused[/+] on Dec 7, 2019
  5. gasparsigma commented on Jun 26, 2020

    @gasparsigma
    Thiago Camargo (gasparsigma)
    on Jun 26, 2020
    More actions

    Please implement this. We have disabled unused local vars ts 6133 because one of our core async libraries uses tuples instead of objects.

    Here's a bit more concrete example

    Reacted by Samuel Holt and Will
  6. TheEdda commented on Jul 9, 2020

    @TheEdda
    Edda (TheEdda)
    on Jul 9, 2020
    More actions

    I'd also like this. I come from Rust exactly as Zane Hitchcox (@zwhitchcox) mentions, and was wondering why the compiler is yelling at me for having unused underscored variables in the middle of a 8-element destructured tuple.

    Moreover, I feel like with the addition of Variadic Tuple Types in Typescript 4 tuples are going to find much more use, as that is exactly why I have an 8-element typed tuple in the first place - it's constructed by a builder and correctly typed thanks to variadic type parameters.

    Reacted by Samuel Holt and Will
  7. samholt commented on Jul 14, 2020

    @samholt
    Samuel Holt (samholt)
    on Jul 14, 2020
    More actions

    I would also like this feature as well

  8. Ryan Cavanaugh (RyanCavanaugh)
    added
    In DiscussionNot yet reached consensusNot yet reached consensus
    and removed
    Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureThis means we'd like to hear from more people who would be helped by this feature
    on Jul 14, 2020
  9. Daniel Rosenwasser (DanielRosenwasser)
    added
    help wanted
    on Nov 2, 2020
  10. Daniel Rosenwasser (DanielRosenwasser)
    added this to the Backlog milestone on Nov 2, 2020
  11. Daniel Rosenwasser (DanielRosenwasser)
    added
    good first issue
    on Nov 2, 2020
  12. DanielRosenwasser commented on Nov 2, 2020

    @DanielRosenwasser
    Daniel Rosenwasser (DanielRosenwasser)
    on Nov 2, 2020
    Member
    More actions

    Seems reasonable, we'd take a PR on this!

    Reacted by Jean Paul Cornejo
  13. Daniel Rosenwasser (DanielRosenwasser)
    added
    CommittedThe team has roadmapped this issueThe team has roadmapped this issue
    and removed
    In DiscussionNot yet reached consensusNot yet reached consensus
    on Nov 2, 2020
  14. Daniel Rosenwasser (DanielRosenwasser)
    modified the milestones: Backlog, TypeScript 4.2.0 on Nov 17, 2020
  15. TypeScript Bot (typescript-bot)
    added
    Fix AvailableA PR has been opened for this issueA PR has been opened for this issue
    on Nov 17, 2020
  16. Nathan Shively-Sanders (sandersn)
    closed this as completedin #41378on Jan 12, 2021
  17. Daniel Rosenwasser (DanielRosenwasser)
    added
    FixedA PR has been merged for this issueA PR has been merged for this issue
    on Feb 12, 2021
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

    CommittedThe team has roadmapped this issueThe team has roadmapped this issueFix AvailableA PR has been opened for this issueA PR has been opened for this issueFixedA PR has been merged for this issueA PR has been merged for this issueSuggestionAn idea for TypeScriptAn idea for TypeScript

    Type

    No type

    Projects

    No projects

      Milestone

      • TypeScript 4.2.0

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.