Skip to content

Declaring a subclass before its superclass passes compilation but causes runtime error #8885

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
gilboa23 opened this issue May 30, 2016 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@gilboa23
Copy link

TypeScript Version:

1.8.10

Code

// SubClass is declared before its SuperClass
class SubClass extends SuperClass {
}

class SuperClass {
}

generated code:

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 __());
};
var SubClass = (function (_super) {
    __extends(SubClass, _super);
    function SubClass() {
        _super.apply(this, arguments);
    }
    return SubClass;
}(SuperClass));
var SuperClass = (function () {
    function SuperClass() {
    }
    return SuperClass;
}());

Expected behavior:
The order of class declarations in TypeScript should not be important. As evidence, the above code passes compilation and thus should not cause runtime error

.

Actual behavior:
Runtime error is thrown Uncaught TypeError: Cannot read property 'prototype' of undefined on line 4.

The reason is that in the call to __extends(SubClass, _super) on line 7 the _super variable is undefined, but the __extends function checks for strict null (b === null on line 4) and thus fails on undefined.

@basarat
Copy link
Contributor

basarat commented May 31, 2016

superclass passes compilation but causes runtime error

A fairly common error https://basarat.gitbooks.io/typescript/content/docs/tips/outFile.html

Similar to a user writing the following JavaScript:

console.log(foo.toString()); // Expecting it to work
var foo = 123;

Sadly you just have to be cautious about it for now. Of course it could be made an error (just isn't yet) 🌹

@Arnavion
Copy link
Contributor

Arnavion commented Jun 1, 2016

Duplicate of #6526 which was merged into #5207

Edit: Also

The order of class declarations in TypeScript should not be important. As evidence, the above code passes compilation and thus should not cause runtime error

is against ES semantics. The runtime behavior is correct. What's missing is the compile-time error.

@mhegazy mhegazy added the Duplicate An existing issue was already created label Jun 7, 2016
@mhegazy mhegazy closed this as completed Jun 7, 2016
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants