Typescript: Allow wildcards in destructuring assignments.

Created on 3 Jul 2019  Β·  5Comments  Β·  Source: microsoft/TypeScript

Search Terms


destruct*
wild*

Suggestion


I would like TypeScript to port the wildcard destructuring feature from Haskell.

Use Cases

The examples below throw errors due to redefining _ in the existing implementation. Currently, we have to define named placeholders which draw unnecessary attention to themselves and require an exception in the linter implementations for no-unused-vars. The Haskell approach is much cleaner.

Examples

  1. const [_, valICareAbout, _] = someFunc();
  2. const eventHandler = (_, _, value) => doSomethingWithValue(value);

Checklist

My suggestion meets these guidelines:

  • [?] 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.)
  • [?] This feature would agree with the rest of TypeScript's Design Goals.
Out of Scope Suggestion

Most helpful comment

Doesn't JS/TS already support omitting a variable when you destructure? Like

const [ , valICareAbout, ] = someFunc();

All 5 comments

This should be taken up with es-discuss / TC39. We didn't create destructuring syntax

Are you saying that this feature does not "agree with the rest of TypeScript's Design Goals"? I assumed this would be fine for TypeScript, as const [_, valICareAbout, _] = someFunc(); TS would just compile down to const [a, valICareAbout, b] = someFunc(); ES.

We're trying to scope TypeScript's additions to those necessary to add static typing to JavaScript. This isn't one of those things, it'd just be new sugar for existing behavior that the TC39 committee created. If TC39 wanted to have duplicated destructuring identifiers cancel each other out, or have _ just evaporate, they could have written it that way, but they didn't, and they're in charge of the behavior of the language.

Doesn't JS/TS already support omitting a variable when you destructure? Like

const [ , valICareAbout, ] = someFunc();

You're too practical @jcalz β™₯

Was this page helpful?
0 / 5 - 0 ratings