6 TypeScript tips to turn you into a WIZARD
Matt Pocock
What's inside
6 thematic sections
Overview
You’ll be able to decide when to enforce data propagation versus API ergonomics, and how optionality choices affect correctness. You’ll know which utility types belong to object shapes versus unions—and why misusing them collapses types into nonsense. Finally, you’ll understand how to manufacture clean, expressive, and fully discriminated unions using mapped types, key remapping, indexing tricks, and display helpers without sacrificing developer experience.
Categories with 6 TypeScript tips to turn you into a WIZARD
6 TypeScript tips to turn you into a WIZARD
Notes with 6 Sections
In TypeScript, a property can be either key optional (using ?) or value optional (required key but value may be undefined), and these two forms behave differently in function calls.
Key optional means the property can be omitted entirely when calling a function, while value optional means the property must be present but can explicitly be undefined. This affects whether TypeScript forces you to pass a value through a chain of functions, preventing accidental omission.
Instead of making the trace ID key optional, we make it value optional. Now trace ID is required, but it can be undefined.
Choosing between key optional and value optional is a trade-off between enforcing value propagation through internal code and keeping the public API cleaner.
Value optional forces each function in a chain to pass the value along, ensuring it isn’t silently dropped, while key optional makes the exported API look simpler by not requiring explicit undefined arguments. The author prefers value optional in application code where enforcement matters more than API prettiness.
Think about key versus value optional in the context of either make your API look pretty with key optional or guarantee that a value is passed, even if it's undefined, with value optional.
In these notes
- Thematic Sections
