O'Reilly logo

Mastering TypeScript by Nathan Rozentals

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Runtime type checking

Although the TypeScript compiler generates compilation errors for incorrectly typed code, this type checking is compiled away in the generated JavaScript. This means that the JavaScript runtime engine knows nothing about TypeScript interfaces or generics. So how can we tell at runtime whether a class implements an interface?

JavaScript has some functions that we can use when dealing with objects, that will tell us what type an object is, or if one object is an instance of another. For type information, we can use the JavaScript typeof keyword, and for instance information, we can use instanceof. Let's have a look at what these functions return, given some simple TypeScript classes, and see if we can use these to tell whether ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required