Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I determine if two objects are of the same type (typescript)

Tags:

typescript

How can I determine if two objects are of the same type (ie the same class)? The objects can be any of about 20 different classes so I don't want a giant test going, both instanceof A, both instanceof B, ... But there's no GetType()/getClass() in typescript.

thanks - dave

like image 442
David Thielen Avatar asked Oct 31 '25 10:10

David Thielen


1 Answers

Just use the constructor property. Reference : http://basarat.github.io/this-and-prototype/#/reflection

class Animal {}
class Bird extends Animal {}


var animal = new Animal();
var bird = new Bird();

console.log(animal.constructor == Animal); // true 
console.log(bird.constructor == Bird); // true
like image 95
basarat Avatar answered Nov 03 '25 02:11

basarat