Skip to content
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

Dynamic class from TypeScript can't be used on NodeJS #23553

Closed
unional opened this issue Apr 20, 2018 · 3 comments
Closed

Dynamic class from TypeScript can't be used on NodeJS #23553

unional opened this issue Apr 20, 2018 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@unional
Copy link
Contributor

unional commented Apr 20, 2018

tsc: 2.8.3

// dynamic.ts
export function dynamic(subject) {
  return class extends subject {
    extended = true
  }
}

Transpiles to

"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = Object.setPrototypeOf ||
        ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
        function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
exports.__esModule = true;
function dynamic(subject) {
    return /** @class */ (function (_super) {
        __extends(class_1, _super);
        function class_1() {
            var _this = _super !== null && _super.apply(this, arguments) || this;
            _this.extended = true;
            return _this;
        }
        return class_1;
    }(subject));
}
exports.dynamic = dynamic;

When using in NodeJS 9.4.0 (and likely many versions before this):

const d = require('./dynamic')

class Foo { foo() { return 'foo' } }

const s = d.dynamic(Foo)
new s() // throws

Error message:
TypeError: Class constructor Foo cannot be invoked without 'new'
due to this line:

 _super.apply(this, arguments)
@DanielRosenwasser
Copy link
Member

Duplicate of #17088 and a proposal exists at #15397.

@unional
Copy link
Contributor Author

unional commented Apr 20, 2018

Thanks.
Want to point out that while the cause is the same as #17088,
this applies to all classes defined in JavaScript, not just build-in classes like Error and Map.

Also, the Object.setPrototypeOf(this, new.target.prototype) workaround does not work here.

unional added a commit to mocktomata/mocktomata that referenced this issue Apr 20, 2018
Transpiling to es2015 is needed to support JS class, due to:
microsoft/TypeScript#23553
unional added a commit to mocktomata/mocktomata that referenced this issue Apr 20, 2018
Transpiling to es2015 is needed to support JS class, due to:
microsoft/TypeScript#23553
@mhegazy mhegazy added the Duplicate An existing issue was already created label Jul 17, 2018
@typescript-bot
Copy link
Collaborator

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants