facebook / facebook/flow

Flow fails to handle simple destructuring syntax

Open
#5,822 2 comments 5 reactions 0 assignees View on GitHub
destructuring
Dominant language
Rust
Stars
22.3k
Forks
1.9k
PR merge metrics
No merged PRs in 30d

Description

The code assigns from the property of one object (`original`) to the property of another one (`target`), which is perfectly legal. You just need parentheses around the whole thing to prevent the runtime from interpreting the opening "{" character as a code block when there is no `let` or `const` to create a new variable but use an existing one):

[Flow Try link](https://flow.org/try/#0MYewdgzgLgBFCGAnA5gUygLhgbxgMxBCzAFcBbAI1UQBoYKljyrEYBfGAXh30KwEY6DRFgBM7ANwAoUJFghEAS2SKw8ADZZcZMk0rU6YMHpbsuPHVgAsow8ZijxbaVIAU23XCRooAOgIgZtwKyqoaAJTSQA)

```js
const target: { foo: number, bar: number } = { foo: 1, bar: 2 };
const original: { mm: number, nn: number } = { mm: 42, nn: 22 };
({ mm: target.foo } = original);
```

```
4: ({ mm: target.foo } = original);
^ unsupported expression pattern in destructuring
```

It only works if I use a(n existing) variable instead of an (existing) object ([Flow Try link for working example](https://flow.org/try/#0DYUwLgBAzg3AUHAxgewHZUmAhgJwObgBcEA3hAGbLLGoCuAtgEYg4A0EjuNDzOEAvhAC8pClWIBGdpxzEATAPgp0kZDgCWedaizBiZevW5MW7VKmO8Bw0YeIAWOWYsQ5C-vDgAKA0ejWRNU1tXQBKeCA)).

[For comparison, here is what **Typescript** does.](https://www.typescriptlang.org/play/#src=const%20target%3A%20%7B%20foo%3A%20number%2C%20bar%3A%20number%20%7D%20%3D%20%7B%20foo%3A%201%2C%20bar%3A%202%20%7D%3B%0D%0Aconst%20original%3A%20%7B%20mm%3A%20number%2C%20nn%3A%20number%20%7D%20%3D%20%7B%20mm%3A%2042%2C%20nn%3A%2022%20%7D%3B%0D%0A%0D%0A(%7B%20mm%3A%20target.foo%20%7D%20%3D%20original)%3B%0D%0A) It has no problems with the same code.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.