TypeScript Patterns I Use Every Day
Isaac Oyelowo
@AlmightyScopes
Practical TypeScript patterns that have improved my code quality and developer experience over the years.
TypeScript Patterns I Use Every Day
After years of writing TypeScript, these are the patterns I reach for most often.
1. Discriminated Unions
Perfect for handling different states in your application:
type AsyncState<T> =
| { status: 'idle' }
| { status: 'loading' }
| { status: 'success'; data: T }
| { status: 'error'; error: Error };2. Branded Types
Add type safety to primitive values:
type UserId = string & { __brand: 'UserId' };
type PostId = string & { __brand: 'PostId' };3. Const Assertions
Get literal types from objects:
const ROUTES = {
home: '/',
about: '/about',
} as const;4. Utility Types
Master the built-in utility types like Pick, Omit, Partial, and Required.
These patterns have saved me countless hours of debugging and made my code more maintainable.
Written by Isaac Oyelowo
Obsessive problem-solver disguised as a software engineer. I write about building products, software architecture, and lessons learned along the way.
Stay in the Loop
Get notified when I publish new articles. No spam, just quality content about software development and building products.
No spam. Unsubscribe anytime.
Discussion
Join the Conversation
Have thoughts on this post? I'd love to hear from you! Comments are coming soon via GitHub Discussions.
Share your thoughts on Twitter