Object.{pick,omit}

Just to clarify, are you two (@lightmare and @aleen42) proposing these bracket syntaxes in addition to the full.{a, b} syntax that @jamiebuilds proposed? Or in place of? I presume it's in place of?

I'm going to make a couple of arguments in favor of @jamiebuilds's syntax:

It's built for the most common use case of just picking pre-known properties off of an object. You don't have to use strings (but we can still add support for dynamic key if wanted. Similar to how object literals and destructuring are built for the most common use case of non-dynamic keys, but have support for dynamic keys)

It could be extended to support other nice features, such as renaming while picking the properties off, or setting default values if the properties don't exist. Hopefully, you can see a parallel with object destructuring. With object destructuring, we pick properties off and put them in the local namespace. With this pick syntax, we pick properties off and put them in a new object.

It's actually got a minor prior art in Microsoft's Bosque language as well (see here), which has an object pick syntax that's exactly the same.

(I've actually started a thread in the past which suggested having this kind of syntax here, which is why I like it so much)

// pick a, b, and c off
// if b doesn't exist, set it to 2
// rename c to C
const newObj = oldObj.{ a, b = 2, c: C }

// Example of dynamic keys
// (again, do you see the parallel with object destructuring?)
const anotherNewObj = something.{ x, [key1], [key2]: renamedKey }
1 Like