Updated April 6, 2023
Introduction to TypeScript instanceof
The Typescript instanceof is one of the operators, and it is used to determine the specific constructor, and it will be creating the object of the classes. It will call the methods with the help of instance like that if we use an interface that can be implemented and extended through the classes. It will determine the property, and its chain will be added to the object it checks the actual condition of the prototype. It returns the Boolean conditions also the name of the variables on its left-hand side included the function of the classes.
Syntax
The instanceof operator is the keyword, and it is mainly used for the object to validate it’s a type or not, and it is followed up through the constructor function. The keyword uses and focuses on the class areas also, and it can be accessed and utilized in the variables, conditional statements, loops, etc.
function functionName(reference: class)
conditional statements(reference instanceof another class)
----- some logics depends upon the user requirements --------
The above code is the basic syntax for utilizing the instanceof operator in the typescript classes. Using that instance, the methods are called for the specific class, which has been already declared on the class. Even the implementation of the interface is also being referred to the operator with the conditional statements.
How instanceof works in TypeScript?
In Typescript instanceof is one of the operators, and it takes the name of the variables on its left-hand side also the name of the function or class; the interface also initialized on the left side of the class. The instanceof operator is used to validate the objects at runtime whenever we want to create the objects at the specific classes; also, it always returns the boolean values like true or false statements if the required class need to create the instance and it requires to authenticate with the other code logics. Basically, the instanceof is the advanced type of the typescript; meanwhile, the operator is tested to see if the prototype property for the class-based constructors appears anywhere in the objects’ prototype chain.
The different scopes have a different set of executions in run time environments; also, it accepts compile-time scripts if the validation is failed for the specific class instance, then it throws the error in the compile-time itself sometimes the operator belongs to execute with the runtime mode in that it throws exceptions on the output console. While we evaluate the class reference using conditional statements, it validates the actual class instance and new class reference.
Examples of TypeScript instanceof
Different examples are given below:
Example #1
function demo(exam, exam1, exam2) {
this.exam = exam;
this.exam1 = exam1;
this.exam2 = exam2;
const demo1 = new demo('first', 'second', 'third');
console.log(demo1 instanceof demo);
console.log(demo1 instanceof Object);
function sample(vars, vars1, vars2, vars3, vars4, vars5, vars6, vars7){
this.vars = vars;
this.vars1 = vars1;
this.vars2 = vars2;
this.vars3 = vars3;
this.vars4 = vars4;
this.vars5 = vars5;
this.vars6 = vars6;
this.vars7=vars7;
const demo2 = new sample('four fsh35 hasd has has', 'secondfive', 'six', 'hs','js','sd','sde','sad sd swf fg fd');
console.log(demo2 instanceof demo);
console.log(demo2 instanceof Object);
Output: