Back to articles
TypeScript Tricks I Actually Use Day to Day

TypeScript Tricks I Actually Use Day to Day

via Dev.toAbdulmalik Muhammad

I've been writing TypeScript for a few years now across React Native, Node.js, and a bunch of different product types. And there's a gap between what the docs teach you and what you actually end up reaching for every day. Here are the patterns I keep coming back to. Discriminated unions for state management This one changed how I model data. Instead of a bunch of optional fields that may or may not exist, you define each state explicitly. type RequestState = | { status : " idle " } | { status : " loading " } | { status : " success " ; data : User } | { status : " error " ; message : string }; Now TypeScript knows exactly what's available in each case. No more data might be undefined checks scattered everywhere. satisfies instead of direct type annotation This one's newer but I use it a lot now. The difference is subtle but useful. const config = { theme : " dark " , language : " en " , } satisfies Record < string , string > ; With satisfies , you get type checking without losing the li

Continue reading on Dev.to

Opens in a new tab

Read Full Article
1 views

Related Articles