Skip to content

DOM's Event is not extensible via class syntax #17156

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
MadaraUchiha opened this issue Jul 13, 2017 · 7 comments
Closed

DOM's Event is not extensible via class syntax #17156

MadaraUchiha opened this issue Jul 13, 2017 · 7 comments
Labels
Duplicate An existing issue was already created

Comments

@MadaraUchiha
Copy link

MadaraUchiha commented Jul 13, 2017

TypeScript Version: 2.3.4

Code

class MyEvent extends Event {
    private foo: string;
    constructor() {
        super('hello');
        this.foo = 'bar';
    }
}

const ev = new MyEvent();

Expected behavior:
Code compiles and runs.

Actual behavior:
Code compiles, but Event has a runtime check that prevents calling it directly (without new). This means that TypeScript's compiled code:

// snip
var MyEvent = (function (_super) {
    __extends(MyEvent, _super);
    function MyEvent() {
        var _this = _super.call(this, 'hello') || this; // this throws
        _this.foo = 'bar';
        return _this;
    }
    return MyEvent;
}(Event));

Will throw at the marked line, because _super can't be .called directly.

With normal JS classes, the exact same code (sans the type annotations) work as expected.


P.S. I know that you're supposed to use CustomEvent with the details property, but that's not supported in IE or Safari, and we sadly need those browsers, and while it could be resolved with a polyfill for CustomEvent, this is still a piece of code that works with plain JS but does not run with TS compilation.

@MadaraUchiha
Copy link
Author

cc @Linkgoron @benjamingr

@jcalz
Copy link
Contributor

jcalz commented Jul 13, 2017

Essentially a duplicate of the issue in #10853, right? Are you trying to target ES5?

@kitsonk
Copy link
Contributor

kitsonk commented Jul 13, 2017

Essentially yes. Built-ins are not extensible in ES5. It was not something that was introduced until ES2015. There is no down-emit that would work.

@benjamingr
Copy link

@kitsonk I'm not really sure what this has to do with ES2015 - this is not a built in - it's a host object.

Whether or not it can be extended is up to the DOM and not JavaScript.

@jcalz
Copy link
Contributor

jcalz commented Jul 13, 2017

Also see #11304

@benjamingr
Copy link

Yes, this issue looks like a duplicate of #11304 - although that one is about an ES built in and this one is about the DOM.

@mhegazy mhegazy added the Duplicate An existing issue was already created label Aug 22, 2017
@mhegazy
Copy link
Contributor

mhegazy commented Sep 6, 2017

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

@mhegazy mhegazy closed this as completed Sep 6, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 14, 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

5 participants