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 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 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 TypeScript Construct Signatures TypeScript Construct Signatures define the shape of a constructor function, specifying the parameters it expects and the type of object it constructs. They use the new keyword in a type declaration to ensure the correct instantiation of classes or objects.Syntaxtype Constructor = new (param1: Type1, 3 min read TypeScript Assertions Type TypeScript Assertions Type, also known as Type Assertion, is a feature that lets developers manually override the inferred or expected type of a value, providing more control over type checking in situations where TypeScript's automatic type inference may not be sufficient.Syntaxlet variableName: As 2 min read TypeScript InstanceType<Type> Utility Type In this article, we are going to learn about InstanceType<Type> Utility Type in Typescript. TypeScript is a popular programming language used for building scalable and robust applications. In TypeScript, the InstanceType<Type> utility type is used to extract the instance type of a constr 3 min read Like