An update on rest operator ?

James Browning thejamesernator at gmail.com
Thu Aug 3 04:44:42 UTC 2017


I still think it's silly that `[...rest, last]` isn't allowed, the point
that it's "iterable destructuring" is actually irrelevant as if there's a
spread it's always coerced to an array e.g.:

```
function* nums() {
    yield 1
    yield 2
    yield 3
    yield 4
}

const [first, ...rest] = nums()
Array.isArray(rest) // true, it's not an iterator for the rest of the nums,
it's just an Array of [2,3,4]
```

Given the above, it should really just be the case that `[...rest, last]`
would just be the same as destructuring the reversed array, then
re-reversing the rest part e.g.:

```
const [...rest, secondLast, last] = someIterable

// Would be equivalent to

const __arr = Array.from(someIterable).reverse()
const [last, secondLast, ...rest] = __arr
rest.reverse() // Reverse it back to correct order
```

---

Really the only contentious case is if it's in the middle because then you
have to decide which direction to consume from e.g.

```
const [a, ...rest, c] = [1]

// Possible values for a, c
// 1, 1
// 1, undefined
// undefined, 1
```

Personally I'd be quite happy to *at least* get the `[...rest, last]` case
even if the middle case couldn't be agreed upon, but someone in tc39 would
need to champion it.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20170803/8c447dd5/attachment-0001.html>


More information about the es-discuss mailing list