Forget theoretical type gymnastics. These are the patterns our dev team reaches for every single day when building real production apps.
1. Discriminated Unions for State Machines
When you have a value that can be in one of several mutually exclusive states, reach for a discriminated union rather than a nullable mess. The compiler will tell you when you've forgotten to handle a case.
2. Const Assertions for Config Objects
When you have a configuration object and you want its values to be literal types rather than broadened primitives, use `as const`. This is especially useful for route definitions and event names.
3. The satisfies Operator
Introduced in TypeScript 4.9, `satisfies` lets you validate that a value matches a type while keeping the narrowest possible type inference. Use it when you want type safety without losing specificity.
4. Template Literal Types
Template literal types let you construct union types from string patterns. They're great for event names, CSS property keys, and anything else with a predictable naming convention.
5. Generic Constraints with keyof
When you write a generic function that operates on object properties, use `keyof` constraints to get type-safe property access. This is the pattern behind most utility types in TypeScript's standard library.
2nd year CS student specialising in frontend. Leads the DevSoc App project team and runs the web development workshop series.
