js conditional object key

JavaScript
const object = {
  ...bool && { foo: 2 },
}
Source

Also in JavaScript: