JavaScript Conditional Object Properties
A technique for conditionally adding a property to an object.
const obj = {
...(isTrue && {a: 'a'})
}
// if isTrue is true
obj = {a: 'a'}
// if isTrue is false
obj = {}

This is a pattern I’ve found really useful, but during a code review a brilliant teammate discovered the following.
- There’s no problem when you combine a boolean with object destructuring (the typical case), but
- when you use false, you get the error below. The error says you can’t use the spread operator because it’s not an object type.

My guess is that internally TypeScript handles boolean and false differently. The fact that it warns you ahead of time, before compilation, about these minor (?) things that could cause bugs proves that TypeScript really is a wise (?) language.
Aside from that, I went into the TypeScript git repo for the first time to dig into this, but I came away with nothing but a sense of despair….
20220224
Leave a comment