[Proposal] Optional spreading

# Scott Rudiger (a month ago)

I like it; code seems cleaner to me with its use. However, since the syntax is so similar to optional chaining, it's too bad your goal with this sample is to check for falsey values rather than nullish values.

[ 1, ?...(condition && [2, 3]), // no extras:) 3, ]

# Jordan Harband (a month ago)

Since you can do this now with:

[
  1,
  ...(condition ? [2, 3] : []),
  3,
]

and object spreading already handles this for you, is extra syntax really needed?

# Beknar Askarov (a month ago)

Indeed. I see your point.

But it really needs to be falsy check. Since falsy values are not "spreadable"

# Beknar Askarov (a month ago)

@Scott My mistake empty string [...""] is spreadable. But not sure if it is desirable behavior.

# Beknar Askarov (a month ago)

And it does result in empty array

[...''] -> []

So not much difference even if it passes through. Everything else would result in error if check was only nullish

# Cyril Auburtin (a month ago)

Like others said, this idea is good, but I think we can already achieve something decent and succinct enough

arrays:

// group by name, and accumulate data:
arr.reduce((m, {name, data, ...o}) => m.set(name, {...o, data:
[...m.get(name)?..data||[], ...data]}), new Map())

objects: {foo: 1, ...cond && {bar: 2}, qux: 3} gist.github.com/caub/7494b4391c2d62c49b565d2cfc2c0c1f#file

# Beknar Askarov (a month ago)

@Scott Rudiger After thinking more about it. I would not like to conflict with semantics of optional chaining and null coalescing operator. So in order to not confuse people, maybe introduce two types of optional spread operators

  1. ?... - Do not spread if nullish. Note nullish. Else try to spread. Signature Array: [?...(nullish | Iterable)]; Signature Object: {?...(nullish | object)};

  2. !... - Do not spread if false. Note FALSE not falsy. Else try to spread. Signature Array: [!...(false | Iterable)]; Signature Object: {!...(false | object)};

I think this can be an option to avoid consfusion

Why not !... to check for all falsy and do not spread of falsy? To avoid confusion and make developers to know their types

For example:

[!...(0 ?? [1 , 2])]; // throws TypeError. number 0 is not iterable

So better will be to use:

[!...(!!0 ?? [1 , 2])];
// OR
[?...(0 ?? [1 , 2])];

What do you think?

# Herby Vojčík (a month ago)

On 23. 8. 2019 16:24, Beknar Askarov wrote:

@Scott Rudiger After thinking more about it. I would not like to conflict with semantics of optional chaining and null coalescing operator. So in order to not confuse people, maybe introduce two types of optional spread operators

  1. ?... - Do not spread if nullish. Note nullish. Else try to spread. Signature Array: [?...(nullish | Iterable)]; Signature Object: {?...(nullish | object)};

  2. !... - Do not spread if false. Note FALSE not falsy. Else try to spread.

I read

!...foo

as

!(...foo)

that is, logical not. I'd tip it already works that way. In which case no go, break compat.

Herby

Signature Array: [!...(false | Iterable)]; Signature Object: {!...(false | object)};

I think this can be an option to avoid consfusion

Or add a new one. :-(