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 Instanceof Operator P pankajbind Follow Improve Article Tags : JavaScript Web Technologies 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 JavaScript Instanceof Operator The instanceof operator in JavaScript is used to check the type of an object at run time. It returns a boolean value if true then it indicates that the object is an instance of a particular class and if false then it is not. Syntax:let gfg = objectName instanceof objectTypeParameters: objectName: S 2 min read Like