TypeScript: TypeError b - undefined

When I try to create inheritance in TypeScript, the following JavaScript is 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 needs to be generated. But the problem is that when executed, Firefox gives this message:

 TypeError: b is undefined 

In Chrome, the error looks a little different, but it seems to have the same origin:

 Uncaught TypeError: Cannot read property 'prototype' of undefined 

An 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 already tried various ways to compile the code, but the result is always the same

+5
source share
1 answer

To get rid of the error, you must place a Medium class declaration in front of one of the Movie .

Please note that the resulting js code is not just function definitions. These are functions and variables. It matters. Because you have both declarations and expressions. You can read more about this and why with expressions in js order you can read in this excellent post: JavaScript function declaration and evaluation order

+3
source

Source: https://habr.com/ru/post/1246789/


All Articles