TypeScript: TypeError b is undefined

10,386

To get rid of error you must put Medium class declaration before one of Movie.

Note that the resulting js code is not just function definitions. It is functions and variables. That make all the difference. Because you have both declarations and expressions. More on this matter and why with expressions in js order does matter you can read in this excellent post: JavaScript function declaration and evaluation order

Share:
10,386
Bajellor
Author by

Bajellor

Updated on June 19, 2022

Comments

  • Bajellor
    Bajellor almost 2 years

    When I am trying to create inheritance in TypeScript the following JavaScript gets generated:

    var __extends = (this && this.__extends) || function (d, b) {
        for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
    

    Which looks exactly like the one that should be generated. But the Problem is that on execution Firefox gives this Message:

    TypeError: b is undefined
    

    In Chrome the error looks a bit different, but seems to be of the same origin:

    Uncaught TypeError: Cannot read property 'prototype' of undefined
    

    The implementation in typescript looks like this

    class Movie extends Medium {
    //Some Fields
        constructor(title: string, description: string, ageRestriction: AgeRestriction, isBluRay: boolean) {
            super(title, description, ageRestriction);
            this.isBluRay = isBluRay;
        }
    }
    
    class Medium implements IMedium {
    //Getters, Setters and Fields
        constructor(title: string, description: string, ageRestriction: AgeRestriction) {
            this.title = title;
            this.description = description;
            this.ageRestriction = ageRestriction;
        }
    }
    

    I have already tried various ways of compiling the code, but the result is always the same