In union types, you can always use instance of as well to narrow the branch. The last one we have here is just the custom type guards. Instead of stuffingo functions full of if statements, you can take the logic out of those functions and just put them in their own functions. You could say a function is sandwich that accepts a type of food. And then from that you can just return if any of the type of the in the proper property check,. or the instance of, and that will return a bullion.

Get the Snipd
podcast app

Unlock the knowledge in podcasts with the podcast player of the future.
App store bannerPlay store banner

AI-powered
podcast player

Listen to all your favourite podcasts with AI-powered features

Discover
highlights

Listen to the best highlights from the podcasts you love and dive into the full episode

Save any
moment

Hear something you like? Tap your headphones to save it with AI-generated key takeaways

Share
& Export

Send highlights to Twitter, WhatsApp or export them to Notion, Readwise & more

AI-powered
podcast player

Listen to all your favourite podcasts with AI-powered features

Discover
highlights

Listen to the best highlights from the podcasts you love and dive into the full episode