What are unions and type narrowing?
Answer out loud or in your head first, then compare.
Answer
A union describes several allowed possibilities. For example, a result can be a success with data or an error with a message. Narrowing means checking which possibility you have, so you can safely use the right fields.
If an object has status: 'success', TypeScript can know that data is available. With status: 'error', message will be available. That way I don't need many independent flags and don't accidentally create a “success and error at once” state. The type helps describe the allowed data, but the rules for moving between states still have to be written in the program.
Why it matters: Useful in UI, APIs and handling an agent's work.
Possible follow-ups: How do you force every variant to be handled in a switch?
How did it go? Be honest — “Not yet” puts the question on your review list.
Practise in code
- Approving a specific proposal — runs tests in your browser
Further reading
- TypeScript Handbook: Narrowing(opens in a new tab)Docs; accessed 22.10.2026. Narrowing, discriminated unions and exhaustiveness.