Typescript typeof and instanceOf operator examples

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

What is typeof operator in typescript?

This operator is used to get a string specifying the type of variable. This will very helpful for developers to know the type of the object at runtime.

typeof operator syntax

typeof { operand };

parameter: operand is supplied either as the primitive data type of custom object. possible operand values are 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

instance of operator checks if the object is another object type and returns true. This checking is done at runtime. It checked conditional expressions to check object type.


name instanceof type;

the name is of an object name and the type is of 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


To sum up, Learned A Step by Step example for Typescript typeof, instanceOf operator tutorials programs syntax.