google / google/closure-compiler

Feature - use correct Event subclasses for all kinds of events

Open
#508 11 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
7.7k
Forks
1.2k
Avg merge
2d 12h
Merged PRs (30d)
6

Description

Given the code -

``` js
window.addEventListener("message", handler, false);
```

`handler` would be called with a `MessageEvent` argument.

Closure Compiler assumes that any `handler` in `addEventListener` takes an `Event` argument, which is incorrect and leads to false warnings (see #506).

Perhaps Closure Compiler can infer the type of event using the event type argument?

Of course, this only works for explicit cases like the one mentioned above, but will not work for other cases like a generic event listener adding function (like `$.fn.on`) - but I think this is fine.

(Same for `attachEvent`, I guess)

After all, more type checking leads to better warnings which leads to less bugs.

This could be implemented as JSDocs maybe, or as EventExterns of some sort that parses this -

``` js
/** @param {string} type
@param {function(!MessageEvent)} handler
@param {boolean=} opt_capture */
Window.prototype.addEventListener = function (type="message", handler, opt_capture);
```

Just an idea, there are probably other ways or syntax.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.