How to use Type Guards in TypeScript ? Last Updated : 24 Jan, 2025 Comments Improve Suggest changes Like Article Like Report Here are the methods to use type guards in TypeScript:1. Using typeof Type GuardsThe typeof operator checks the type of a variable, primarily for primitive types like string, number, boolean, etc. JavaScript function processValue(value: string | number) { if (typeof value === 'string') { console.log(value.toUpperCase()); } else { console.log(value.toFixed(2)); } } processValue('Hello'); processValue(123.456); If value is a string, toUpperCase() is called.If value is a number, toFixed(2) is called.Output:HELLO123.462. Using instanceof Type GuardsThe instanceof operator checks if an object is an instance of a specific class or constructor function. JavaScript class Dog { bark() { console.log('Woof!'); } } class Cat { meow() { console.log('Meow!'); } } function makeSound(animal: Dog | Cat) { if (animal instanceof Dog) { animal.bark(); } else { animal.meow(); } } const myDog = new Dog(); const myCat = new Cat(); makeSound(myDog); makeSound(myCat); If animal is an instance of Dog, bark() is called.If animal is an instance of Cat, meow() is called.Output:Woof!Meow!3. Using the in OperatorThe in operator checks if a property exists in an object. JavaScript interface Bird { fly: () => void; } interface Fish { swim: () => void; } function move(animal: Bird | Fish) { if ('fly' in animal) { animal.fly(); } else { animal.swim(); } } const pigeon: Bird = { fly: () => console.log('Flying') }; const salmon: Fish = { swim: () => console.log('Swimming') }; move(pigeon); move(salmon); If animal has a fly method, it's a Bird, and fly() is called.If animal has a swim method, it's a Fish, and swim() is called.Output:FlyingSwimming4. Using User-Defined Type GuardsCustom type guard functions use type predicates to narrow down types. JavaScript interface Car { drive: () => void; } interface Boat { sail: () => void; } function isCar(vehicle: Car | Boat): vehicle is Car { return (vehicle as Car).drive !== undefined; } function operate(vehicle: Car | Boat) { if (isCar(vehicle)) { vehicle.drive(); } else { vehicle.sail(); } } const sedan: Car = { drive: () => console.log('Driving') }; const yacht: Boat = { sail: () => console.log('Sailing') }; operate(sedan); operate(yacht); The isCar function checks if vehicle has a drive method.In operate, isCar determines whether to call drive() or sail().Output:DrivingSailing Comment More infoAdvertise with us Next Article How to use Type Guards in TypeScript ? A akhilboy Follow Improve Article Tags : JavaScript Web Technologies TypeScript Similar Reads How to Use Partial Types in TypeScript ? Partial types in TypeScript offer a versatile solution for creating new types with a subset of properties from existing types. This feature proves invaluable in scenarios where you need to work with incomplete data structures or define flexible interfaces. Below are the approaches to using partial t 2 min read TypeScript Narrowing typeof type guards In this article, we are going to learn about Narrowing typeof type guards. TypeScript is a popular programming language used for building scalable and robust applications. In TypeScript, the typeof type guard allows you to narrow down a variable's type based on the result of the typeof operator. Thi 3 min read How to Get a Variable Type in TypeScript Understanding how to effectively ascertain the type of a variable in TypeScript is important for maintaining type safety and ensuring code robustness. In this article, we'll explore various approaches to determine the type of a variable, ranging from basic JavaScript operators to TypeScript-specific 2 min read How to Check Types in Typescript? Checking types in TypeScript involves methods like typeof for primitive types, instanceof for class instances, and custom type guards for complex type validation. These techniques help ensure variables are correctly typed, improving code safety, and readability, and preventing runtime errors.Here ar 3 min read How to Return a Union Type in TypeScript ? In TypeScript, a union type is a powerful way to express a variable that can be one of several types. Union types are used when a function or variable is expected to support multiple types of input. Union types are defined using the pipe (|) symbol between two or more types. This indicates that a va 4 min read Like