Prototypal inheritance

We might be wondering how the extends keyword works. Let's create a new TypeScript class, which inherits from the Person class, to understand it:

class SuperHero extends Person { 
    public superpower: string; 
    public constructor( 
        name: string, 
        surname: string, 
        superpower: string 
    ) { 
        super(name, surname); 
        this.superpower = superpower; 
    } 
    public userSuperPower() { 
        return `I'm using my ${this.superpower}`; 
    } }

The preceding class is named SuperHero and extends the Person class. It has one extra attribute (superpower) and method (useSuperPower). If we compile the code, we will notice the following piece of code:

var __extends = this.__extends || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; function ...

Get Learning TypeScript 2.x - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.