Typescript: Destructuring array, ability to explicitly tell the variable is unused

Created on 14 May 2019  路  7Comments  路  Source: microsoft/TypeScript

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 - https://github.com/microsoft/TypeScript/issues/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.

Checklist

My suggestion meets these guidelines:

  • [x] This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • [x] This wouldn't change the runtime behavior of existing JavaScript code
  • [x] This could be implemented without emitting different JS based on the types of the expressions
  • [x] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • [x] This feature would agree with the rest of TypeScript's Design Goals.
Committed Fix Available Suggestion good first issue help wanted

Most helpful comment

Bump. This is still an issue in 3.7.5.

All 7 comments

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.

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).

Bump. This is still an issue in 3.7.5.

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

I'd also like this. I come from Rust exactly as @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.

I would also like this feature as well

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

Was this page helpful?
0 / 5 - 0 ratings