Skip to content

noUnusedLocals and removing properties with object rest destructuring #12766

Closed
@jkillian

Description

@jkillian

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.

Metadata

Metadata

Assignees

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