Skip to content
TypeScript·Intermediate·Coding·1 min read

How does it make impossible states unrepresentable?

Short interview answer

Instead of independent booleans and optional fields that can contradict each other, define one member for each valid state with only its valid payload. The type system then rejects combinations the domain does not allow.

Example

TypeScript
// Representable but impossible: loading + error + data all setinterface Bad { loading: boolean; error?: string; data?: Data }
type Good =  | { status: 'loading' }  | { status: 'error'; error: string }  | { status: 'loaded'; data: Data };

Key takeaway

Explain the underlying mental model clearly, then support it with a concrete example and its trade-offs.

← Back to Discriminated unions

Related questions