TypeScript·Advanced·Coding·1 min read
What are conditional types?
Short interview answer
A conditional type selects one type or another based on assignability. When the checked type is a naked type parameter, it distributes over unions. Wrapping both sides in tuples can suppress distribution.
Example
type Unwrap<T> = T extends Promise<infer U> ? U : T;type A = Unwrap<Promise<string>>; // stringtype B = Unwrap<number>; // number
type NonNull<T> = T extends null | undefined ? never : T;type C = NonNull<string | null>; // string (distributes over the union)Key takeaway
Explain the underlying mental model clearly, then support it with a concrete example and its trade-offs.