Typescript typeof and instanceOf operator examples

You can check another post on Fix for Object is possibly null

What is the typeof operator in TypeScript?

The typeof operator is utilized to obtain a string specifying the type of a variable. This proves to be highly beneficial for developers in determining the type of an object at runtime.

typeof operator syntax

typeof operand ;

parameter: operand: This is provided as either a primitive data type or a custom object. Possible operand values include variables, object variables, and strings.

var msg = "Cloudhadoop";
console.log(typeof msg); // returns string as string format
var variableNumber =16;
console.log(typeof variableNumber); // returns number as string format

var variableBoolean =true;

console.log(typeof variableBoolean); // returns boolean as string format

var variableNull =null;
console.log(typeof variableNull); // returns the object as string format
var variableArray:string\[\]= \["1","2","3","4"\]

console.log(typeof variableArray); // returns object as string format

let variableTyple: \[string, number\]= \["type operator", 20\];

console.log(typeof variableTyple); // returns object as string format

let variableAny: any = 4;
console.log(typeof variableAny); // returns number as string format

let variableUndefined: undefined = undefined;
console.log(typeof variableUndefined); // returns undefined as string format


enum Monday { Sunday, Monday, Tuesday }
let enumVariable: Monday = Monday.Sunday;
console.log(typeof enumVariable); // returns number as string format


var myFunction = new Function('123 + 6');
console.log(typeof myFunction) // outputs 'Function'


var currentDate = new Date();
console.log(typeof currentDate) // outputs 'object'

TypeScript instanceof Operator

The instanceof operator checks if an object is of another object type and returns true. This check is performed at runtime within conditional expressions to verify the object type.

Syntax

name instanceof type;

Where name is the object’s name and type is the object type.

instanceOf Examples

In the below code, the date object name checks against Class and returns.

any variable returns false against the date object

var todaysDate = new Date();
var variableNumber: any = 12;
console.log(todaysDate instanceof Date); // returns true
console.log(variableNumber instanceof Date); // returns false

instanceof class example

It returns false if the undefined type-checked with class,

class ClassExample {}

let classes: Array = \[\];

console.log(classes\[0\]); // returns undefined

console.log(classes\[0\] instanceof ClassExample);

//returns false  undefined is not of ClassExample

classes.push(new ClassExample());

console.log(classes\[0\]); // outpus empty class

console.log(classes\[0\] instanceof ClassExample); // true

instanceof interface example:

This is an example of usage of the interface with the instanceof operator

interface InterfaceExample {}

var interfaceVariable: InterfaceExample = {};

console.log(interfaceVariable instanceof InterfaceExample); // compilation error

class ClassExample {}

var variableClass: ClassExample = {};

console.log(variableClass instanceof ClassExample); // reutnrs false and no compilation error

as you see interface gives ’ InterfaceExample’ only refers to a type, but is being used as a value here error

Conclusion

In conclusion, we have walked through a step-by-step example for TypeScript’s typeof and instanceof operators, understanding their syntax and usage.