Aljen Magat
← Back to Blogs

TypeScript patterns every developer should know

TypeScriptText: SinanDate: 19.Apr 2023Read: 4 Min
TypeScript patterns every developer should know

Start Strict, Stay Strict:

Enable strict mode from project day one. It catches null reference bugs, implicit any types, and unsafe casts at compile time, saving hours of runtime debugging in production.

Model Your Domain:

Use discriminated unions and branded types to represent application states explicitly. Instead of optional fields everywhere, model loading, success, and error as distinct types. Your IDE and compiler become your best reviewers.

Share Types Across the Stack:

With tRPC, Zod, or shared monorepo packages, frontend and backend can share the same type definitions. API contracts stay in sync automatically, eliminating an entire class of integration bugs between client and server.

Comments (9)

Share your thoughts or reply to other readers on this post.

Patrick WalshJun 20, 2024, 5:20 PM

Best TypeScript patterns post I've read this year. Sending to the whole frontend team.

Laura NguyenJan 8, 2024, 10:00 PM

Any tips for migrating a large JS codebase to strict TypeScript incrementally?

Aljen MagatAuthorJan 9, 2024, 4:30 AM

Enable strict per folder, use allowJs initially, and fix types module by module. Don't big-bang it.

Ian CooperOct 2, 2023, 8:45 PM

Branded types for user IDs vs order IDs — small pattern, huge safety win.

Emily ParkJul 19, 2023, 12:00 AM

Shared types with tRPC eliminated half our integration bugs. Great mention.

Sofia KleinJul 19, 2023, 6:10 PM

tRPC + Zod is our default stack now for full-stack TypeScript apps.

Greg HuangMay 10, 2023, 7:30 PM

Strict mode from day one — wish we'd done this on our legacy codebase sooner.

Sofia KleinApr 22, 2023, 5:10 PM

Discriminated unions changed how we model API responses. Clear and practical advice.

Aljen MagatAuthorApr 22, 2023, 11:00 PM

Discriminated unions plus exhaustive switch checks catch so many bugs at compile time.

Leave a comment