DEV Community

Discussion on: Type Narrowing in TypeScript

Collapse
 
pobx profile image
Pobx • Edited

Thank you. It's amazing article. I love your short explanation foreach section. It's make me clear about it. I have question about "Discriminated Unions" and "in operator". Which one I should use for narrow type. now it's make me a little bit confuse because it's may be same thing.

Collapse
 
mainawycliffe profile image
Maina Wycliffe

I would lean with discriminated unions over using the in operator because they are easier to use. With the in operator, you might find yourself writing more complex checks if two or more types have a lot of overlapping properties. For simple check though, you can use them interchangeably, with no issue at all.