Flow: support destructuring, spread of all iterables, not just arrays

Created on 8 Nov 2015  路  7Comments  路  Source: facebook/flow

const [a, b, c] = new Set([1, 2, 3]);

With the above code, I expect there to be no errors, and each binding a, b, c should be number.

Today, I get the following errors:

test.js:2
 2: const [a, b, c] = new Set([1, 2, 3]);
           ^ element 0. Indexable signature not found in
Set. See: [LIB] core.js:376

test.js:2
 2: const [a, b, c] = new Set([1, 2, 3]);
              ^ element 1. Indexable signature not found in
Set. See: [LIB] core.js:376

test.js:2
 2: const [a, b, c] = new Set([1, 2, 3]);
                 ^ element 2. Indexable signature not found in
Set. See: [LIB] core.js:376

relevant spec text.

Most helpful comment

It should also be possible to spread arbitrary iterables, not just arrays:

const set = new Set([1, 2, 3]);
console.log(...set); // 1, 2, 3
test.js:2
  2: const set = new Set([1, 2, 3]);
                 ^^^^^^^^^^^^^^^^^^ constructor call
  3: console.log(...set); // 1, 2, 3
                    ^^^ Set. This type is incompatible with
  3: console.log(...set); // 1, 2, 3
                    ^^^ spread operand

All 7 comments

It should also be possible to spread arbitrary iterables, not just arrays:

const set = new Set([1, 2, 3]);
console.log(...set); // 1, 2, 3
test.js:2
  2: const set = new Set([1, 2, 3]);
                 ^^^^^^^^^^^^^^^^^^ constructor call
  3: console.log(...set); // 1, 2, 3
                    ^^^ Set. This type is incompatible with
  3: console.log(...set); // 1, 2, 3
                    ^^^ spread operand

Meanwhile, any workarounds to ignore this issue for spread with set?

I'm a few months late, but a workaround is to wrap all iterables (set, map, generator, string, custom iterables) in Array.from before spreading them.

Another example, but DOM NodeList should also be supported:

[...document.querySelectorAll('img')].forEach(node => node.remove())

Any idea about the roadmap for this improvement?

It looks like this works now: https://flow.org/try/#0MYewdgzgLgBAHjAvDMBTA7jAyqqAKAbQEYAaGAJjIGYBdASgG4AoUSWATyRgIDo+4azJnnYEADDQBcKAK4BbAEaoATo2GiJ06MoCWYAOZqgA

Let me know if this is still an issue 馃憤

@calebmer that looks like a particular case of destructuring - it looks like destructuring assignments still don't work: https://flow.org/try/#0PQKhCgAIUgBAzANgewO6QM4BcBOBLAYyyhGHHABMBTAxAQxysgDcGWAuSASSypzoBGiKgB5s+AHYBzAHwBucgWQTskANrw8ObAF1IAXhYKlKrOoB0lxroNHwACk3asncXmkBKBfesvMudyk1HS9wIA

Opened as new ticket #8375

Was this page helpful?
0 / 5 - 0 ratings