TypeScript Instanceof Operator Last Updated : 22 Jul, 2024 Comments Improve Suggest changes Like Article Like Report The TypeScript instanceof operator checks if an object is an instance of a specified class or constructor function at runtime. It returns a boolean value: `true` if the object is an instance of the type, and `false` otherwise, ensuring robust type checking during execution.SyntaxobjectName instanceof typeEntityParameters:objectName: It is the object whose type will be checked at the run time.typeEntity: It is the type for which the object is checked.Return Value:It returns true if the object is an instance of the passed type entity. Otherwise, it will return true.Example 1: Using instanceof with TypeScript ClassesThis example demonstrates the basic usage of the instanceof operator with TypeScript classes. JavaScript class Person { name: string; age: number; constructor(name: string, age: number) { this.name = name; this.age = age; } } const person1 = new Person("Pankaj", 20); console.log(person1 instanceof Person); const randomObject: { name: string, job: string } = { name: "Neeraj", job: "Developer" }; console.log(randomObject instanceof Person); Output:true falseExample 2: Using instanceof with TypeScript Constructor FunctionsThis example demonstrates the usage of the instanceof operator with constructor functions in TypeScript. JavaScript function Company (name: string, est: number) { this.name = name; this.est = est; } const GFG = new Company("GeeksforGeeks", 2009); const cmpny2 = { name: "Company2", est: 2010 } console.log(GFG instanceof Company); console.log(cmpny2 instanceof Company); Output:true falseThe instanceof operator is a powerful tool in TypeScript for type checking at runtime. It allows you to verify if an object is an instance of a specific class or constructor function, which can be useful in various scenarios such as type assertions and debugging. By understanding and using instanceof, you can ensure more robust type safety in your TypeScript applications. Comment More infoAdvertise with us Next Article TypeScript Interfaces Type P pankajbind Follow Improve Article Tags : TypeScript Similar Reads Typescript Keyof Type Operator The TypeScript keyof operator is used to get a union of all keys in an object type. Itâs useful when you want to work with the property names of an object in a type-safe way, ensuring only valid keys are used.We can use it to define generic functions that work with any object type, without knowing t 3 min read TypeScript instanceof narrowing Type TypeScript instanceof operator is used for type narrowing, allowing us to check whether an object is an instance of a specific class or constructor function. When we use instanceof in a conditional statement, TypeScript narrows the type of a variable based on the result of the check. Syntax:if (obje 3 min read TypeScript in operator narrowing Type In this article, we will learn about the 'in' operator narrowing Type in Typescript. In TypeScript, the 'in' operator is used to narrow or refine the type of an object within a conditional statement or block. It checks whether a specific property or key exists within an object, and if it does, it na 3 min read TypeScript Interfaces Type TypeScript Interfaces Type offers an alternative method for defining an object's type, allowing for a distinct naming approach. Syntax:interface InterfaceName { property1: type1; property2?: type2; readonly property3: type3; // ... method1(): returnType1; method2(): returnType2; // ...}Parameters:in 2 min read TypeScript Generic Constraints In TypeScript, generic constraints restrict the types that can be used with a generic type by using the extends keyword. This ensures that the generic type adheres to a specific structure or interface, allowing access to certain properties or methods within the generic type.What are Generic Constrai 3 min read Data types in TypeScript In TypeScript, a data type defines the kind of values a variable can hold, ensuring type safety and enhancing code clarity.Primitive Types: Basic types like number, string, boolean, null, undefined, and symbol.Object Types: Complex structures including arrays, classes, interfaces, and functions.Prim 3 min read Like