Chapter 11: Design

Make illegal states unrepresentable

Design your data so that a wrong combination can't even be written down. An order that is both cancelled and shipped should be impossible to express, rather than something every function must remember to check.

Every check you have to remember is one an agent can forget. With the right data shape, the type checker catches the mistake in every session, including the ones nobody reads, and there are fewer branches to test.

Say it to your agent

“Replace the isPaid, isShipped and isCancelled booleans with one status field: a TypeScript union where shipped carries a trackingId and cancelled carries a reason. Then fix every compile error. Don't add new if-checks.”

The workshop

Name a word, or describe what you mean in everyday language.