Skip to content

noUnusedLocals and removing properties with object rest destructuring #12766

Description

TypeScript Version: 2.1.4

Code

function one(){
  const foo = {a: 1, b: 2};
  // 'a' is declared but never used
  const {a, ...bar} = foo;
  console.log(bar);
}

function two(){
  const foo = {a: 1, b:2};
  // '_' is declared but never used
  const {a: _, ...bar} = foo;
  console.log(bar);
}

Expected behavior:
In example one, it's debatable to me if a should be marked as unused. I propose not marking a variable unused if there's an object spread in the same destructuring. This is because currently, object spreads are the only way to get typed removal of object properties. (Mapped types can let you pick properties, but not let you choose properties to remove, so we can't type something like _.omit yet)

In example two, _ should not be marked as unused.

Actual behavior:
In example one, we have no intention of using a, we're only interested in removing it from bar. With --noUnusedLocals enabled, we get a warning that a is declared but never used. This is debatably correct behavior (see above).

In example two, even when we bind it to the name _, we still get an unused local warning.

Activity

  1. added a commit that references this issue on Dec 8, 2016
    81452c5
  2. jkillian commented on Dec 9, 2016

    @jkillian
    Author

    Thanks for the quick fix!

  3. changed the title [-]noUnusedLocals and removing properties with object spread destructuring[/-] [+]noUnusedLocals and removing properties with object rest destructuring[/+] on Sep 21, 2017
  4. locked and limited conversation to collaborators on Jun 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

BugA bug in TypeScriptFixedA PR has been merged for this issue

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions