Optional spread and logical and (&&)
A little clever snippet that comes handy surprisingly often.
const something= {a:1,b:2};
const status = "Sheldonian";
const somethingElse = {
...something,
...(status && { status }),
};
// Output: { a: 1, b: 2, status: 'Sheldonian' }
while
const something= {a:1,b:2};
const status = false;
const somethingElse = {
...something,
...(status && { status }),
};
// Output: { a: 1, b: 2 }
That said, with constructing values I deem it better to represent the null/undefined values in code with the proper type, rather than a lack of something. To think of it, even if gRPC defaults to 0 value of type in the code, the key & value only disappears in transport. My preference is to check the values, not their presence, but it's still a very useful tiny snippet!
Member discussion