AI-powered
podcast player
Listen to all your favourite podcasts with AI-powered features
How to Define a User Defined Type Guard in TypeScript
What is a type eraser and how does it affect the JavaScript code? In TypeScript, when you write a user defined type guard, you write a function that returns true or false. However, the return type of that function is not a Boolean. So in our case, we have a food type that could be a sandwich or a pizza and we needed to write a Typeguard that says this food is pizza. It's kind of weird that it doesn't do that, but I guess it's the limitation of Type Script.