Top 3 Discriminated unions Interview Questions
Browse 3 real-world technical and behavioral interview questions about Discriminated unions. Review scenarios, edge cases, and architectural best practices.
How do you make the compiler fail when somebody adds a new variant to a union you switch on?
A TypeScript exhaustive switch uses a discriminated union and a never check so the compiler fails when a new variant is added but not handled. This turns future union growth into a compile-time error. It also connects discriminated unions to the point an interviewer is testing.
How do you use TypeScript's type system to make invalid states unrepresentable in a service?
You shrink the set of values the compiler accepts: discriminated unions instead of optional-field soup, branded types so one id cannot stand in for another, and a never-typed fallback that fails the build when a variant is added. Types vanish at runtime, so input is parsed by a schema the type is inferred from.
How do you use TypeScript to make a component's props hard to misuse?
A safe TypeScript component props API models legal states with discriminated unions, ties related props together with generics and keeps any out of the public surface.