September 2018
Intermediate to advanced
302 pages
7h 17m
English
TypeScript uses structural typing. To understand what that means, let's look at the following example:
interface Donut { flavour: FLAVOURS;}class ChocolateDonut { flavour: FLAVOURS.CHOCOLATE;}let p: Donut;// OK, because of structural typingp = new ChocolateDonut();
In this example, we first declare the p variable, and then assign a new instance of ChocolateDonut to it. It works in TypeScript. It wouldn't work in Java. Why?
We have never explicitly indicated that ChocolateDonut implements the Donut interface. If TypeScript did not use structural typing, you would need to refactor part of the preceding code to the following:
class ChocolateDonut implements Donut { flavour: FLAVOURS.CHOCOLATE;}
The reasoning behind using structural ...
Read now
Unlock full access