Open freshp86 opened 6 years ago
Looking over that PR, it seems like Bradford's suggestion of aliasing the native EventTarget is a workaround:
I agree it's not as satisfying as updating the default externs, though.
The workaround does not seem to work (I had also tried this before filing this bug). Pasted the contents into a file in Chromium that is currently type checked and here are the errors (you can ignore the filenames)
../../chrome/browser/resources/settings/search_engines_page/search_engines_page.js:8: ERROR - property addEventListener on interface EventTarget is not implemented by type Foo
class Foo extends NativeEventTarget {}
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
../../chrome/browser/resources/settings/search_engines_page/search_engines_page.js:8: ERROR - property dispatchEvent on interface EventTarget is not implemented by type Foo
class Foo extends NativeEventTarget {}
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
../../chrome/browser/resources/settings/search_engines_page/search_engines_page.js:8: ERROR - property removeEventListener on interface EventTarget is not implemented by type Foo
class Foo extends NativeEventTarget {}
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Update: Getting similar errors from the debug service, pasting below:
JSC_INTERFACE_METHOD_NOT_IMPLEMENTED: property addEventListener on interface EventTarget is not implemented by type Foo at line 5 character 0
class Foo extends NativeEventTarget
^
JSC_INTERFACE_METHOD_NOT_IMPLEMENTED: property dispatchEvent on interface EventTarget is not implemented by type Foo at line 5 character 0
class Foo extends NativeEventTarget
^
JSC_INTERFACE_METHOD_NOT_IMPLEMENTED: property removeEventListener on interface EventTarget is not implemented by type Foo at line 5 character 0
class Foo extends NativeEventTarget
^
Ah, good point. I wasn't looking at the warnings.
It's possible to run the compiler with your own externs, but that's a lot poorer UX than just getting the default externs set up correctly. If you wanted to push bradford's workaround further, I think you could redefine those missing methods, e.g. https://goo.gl/atQNrQ, but I agree it's getting pretty ugly.
Created Google internal bug b/119637644
The workaround for this is pretty gross, but the following code does work:
/**
* @constructor
* @implements {EventTarget}
*/
const EventTargetImplementation = window['EventTarget'];
/** @override */
EventTargetImplementation.prototype.addEventListener = window['EventTarget'].prototype.addEventListener;
/** @override */
EventTargetImplementation.prototype.removeEventListener = window['EventTarget'].prototype.removeEventListener;
/** @override */
EventTargetImplementation.prototype.dispatchEvent = window['EventTarget'].prototype.dispatchEvent;
class MyClass extends EventTargetImplementation {}
Not a constructor warning also pops with this :
const allTheEvents = new EventTarget();
JSDoc implements tag fixed it but I got more warnings with addEventListener.
Ugly fix : use extends instead.
/**
* @constructor
* @implements {EventTarget}
*/
var NativeEventTarget = self["EventTarget"];
const allTheEvents = new NativeEventTarget();
allTheEvents.addEventListener(/* */);
allTheEvents.dispatchEvent(new CustomEvent(...
Thanks !
Closure compiler should allow instantiating or extending the native EventTarget class, see [1]. Also see [2] which depicts that this is already implemented in Chrome.
Unfortunately closure compiler throws an error for both these cases.
Even if this is too hard to fix in the default JS compiler externs, there should be a command line flags that allows using a different set of externs to achieve this.
See previous PR about this at https://github.com/google/closure-compiler/pull/2988, which never landed.
[1] https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/EventTarget [2] https://www.chromestatus.com/features/5721972856061952