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

Enhancement - Gaurded Type Promotion #548

Closed
ghost opened this issue Jul 13, 2014 · 3 comments
Closed

Enhancement - Gaurded Type Promotion #548

ghost opened this issue Jul 13, 2014 · 3 comments

Comments

@ghost
Copy link

ghost commented Jul 13, 2014

Dart supports guarded type promotion - could you guys please consider supporting this in Closure Compiler? I'm sure you're aware of the benefits but I will illustrate it below for anyone reading this who might not know what guarded type promotion is. Shamefully, I was completely unaware of it until recently.

/**
 * @param {Event} event
 */
function listener(event) {
  if (event instanceof MessageEvent) {
    // no type casting is required here because instanceof is ensuring the
    // event is a MessageEvent instance
    console.info(event.data)
  }
}

The following, on the other hand, can get really ugly as we all know.

/**
 * @param {Event} event
 */
function listener(event) {
  if (event instanceof MessageEvent) {
    console.info(/** @type {MessageEvent} */ (event.data))
  }
}
@nicks
Copy link
Contributor

nicks commented Jul 13, 2014

have you tried it? we already do this.

@nicks nicks closed this as completed Jul 13, 2014
@ghost
Copy link
Author

ghost commented Jul 13, 2014

have you tried it? we already do this.

Ah yes, it appears you do. My mistake.

@MatrixFrog
Copy link
Contributor

I knew that we did this but I didn't know the term "guarded type promotion" until now. Cool :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants