Radu Catalin-Andrei’s Post

View profile for Radu Catalin-Andrei

🌍 Senior Full-Stack Engineer | B2B SaaS platforms with React, Next.js & Node.js | Headless CMS Integrations | Performance & Multi-Tenant Systems | Scalable Web Architectures | 7+ Years | B2B Contractor

Stop writing the same null check 5 times. Use a discriminated union instead. I spent years writing code like this: if (data && data.user && data.user.id) { ... } Then I started modeling state properly with discriminated unions in TypeScript, and it changed how I structure entire features. Instead of a blob of optional fields, you define each possible state explicitly: type FetchState<T> = | { status: 'idle' } | { status: 'loading' } | { status: 'success'; data: T } | { status: 'error'; message: string } Now TypeScript narrows the type for you. No more defensive ?. chains. No more "wait, can this be null here?" The shape of your data tells the story. It works especially well in React, where UI state maps directly to these cases. One switch on status and each branch is fully typed. The shift in thinking: stop modeling data as "what fields might exist" and start modeling it as "what states can this be in." What patterns do you reach for first when typing async state in TypeScript? #TypeScript #React #WebDevelopment

To view or add a comment, sign in

Explore content categories