Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript error runtime error: Cannot read property 'prototype' of undefined when extending

So I'm getting the above error in the console. It's caused by _super being undefined when it's passed to __extends (in the generated .js).

Here's some test code that can be used to reproduce the error:

//This is the entirety of the file Test.ts module Test {     export class Test1 {         public Name: string;         public Number: number;          constructor() {          }     } } 

Then in a separate file I have a class that inherits from that one:

/// <reference path="Test.ts" /> module Test {     export class Test2 extends Test1 {         constructor() {             super();         }     } } 

The <reference path... shouldn't be needed (and isn't), but I added it to see if it helped (it didn't).

The files are included in the correct order (Test.ts then Test2.ts) via BundleConfig (running with or without optimisations doesn't have any effect).

I am probably being a giant noob, but I haven't the slightest clue what I've messed up. All the other instances of this problem I've found online are from folks using the command line compiler to combine multiple Typescript files into one single file. I'm using the bundler to do that, but even when I don't combine them, I get the exact same issue.

Please help me, I'm at my wits end!

As requested, here's the compiled javascript: Test.js:

//This is the entirety of the file Test.ts var Test; (function (Test) {     var Test1 = (function () {         function Test1() {         }         return Test1;     })();     Test.Test1 = Test1; })(Test || (Test = {})); //# sourceMappingURL=Test.js.map 

Test2.js:

var __extends = this.__extends || function (d, b) {     for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];     function __() { this.constructor = d; }     __.prototype = b.prototype;     d.prototype = new __(); }; /// <reference path="Test.ts" /> var Test; (function (Test) {     var Test2 = (function (_super) {         __extends(Test2, _super);         function Test2() {             _super.call(this);         }         return Test2;     })(Test.Test1);     Test.Test2 = Test2; })(Test || (Test = {})); //# sourceMappingURL=Test2.js.map 
like image 572
Maverick Avatar asked Jun 26 '14 00:06

Maverick


People also ask

How do you fix undefined properties Cannot be read?

To solve the "Cannot read properties of undefined" error, make sure that the DOM element you are accessing exists. The error is often thrown when trying to access a property at a non-existent index after using the getElementsByClassName() method. Copied! const boxes = document.

Can not read the property of undefined typescript?

What Causes TypeError: Cannot Read Property of Undefined. Undefined means that a variable has been declared but has not been assigned a value. In JavaScript, properties and functions can only belong to objects.

What does Cannot read properties of undefined mean?

JavaScript TypeError is thrown when an operand or argument passed to a function is incompatible with the type expected by that operator or function. This error occurs in Chrome Browser when you read a property or call a method on an undefined object .

What is the meaning of Cannot Read property Code of null?

The "Cannot read property 'value' of null" error occurs when: trying to access the value property on a null value, e.g. after calling getElementById with an invalid identifier. inserting the JS script tag before the DOM elements have been declared.


1 Answers

Possible reasons this is happening:

  1. Quadruple-check that BundleConfig is concatenating the files in the correct order. This is by far the most common cause of that error.
  2. Verify you don't have any top-level export directives in Test.ts. This would cause the file to become an external module and Test1 would no longer be visible.

Failing that, you should post the emitted JavaScript to the question so we can diagnose what's causing the issue.

like image 132
Ryan Cavanaugh Avatar answered Oct 08 '22 12:10

Ryan Cavanaugh