google / closure-compiler

A JavaScript checker and optimizer.
https://developers.google.com/closure/compiler/
Apache License 2.0
7.39k stars 1.15k forks source link

Nested union types fail equality check #1837

Open SLaks opened 8 years ago

SLaks commented 8 years ago
/** @constructor @template T */
function EventType(){}

/**
 * @param {string|!EventType<boolean|number>} a
 * @param {!EventType<boolean>} b
 * @return {boolean}
 */
function compare(a, b) {
  return a === b;
}
input0:10: WARNING - condition always evaluates to false
left : (EventType<(boolean|number)>|string)
right: EventType<boolean>
  return a === b;
         ^

The (obviously-incorrect) error only happens if both |s are present.

https://closure-compiler-debugger.appspot.com/#input0%3D%252F**%2520%2540constructor%2520%2540template%2520T%2520*%252F%250Afunction%2520EventType()%257B%257D%250A%250A%252F**%250A%2520*%2520%2540param%2520%257Bstring%257C!EventType%253Cboolean%257Cnumber%253E%257D%2520a%250A%2520*%2520%2540param%2520%257B!EventType%253Cboolean%253E%257D%2520b%250A%2520*%2520%2540return%2520%257Bboolean%257D%250A%2520*%252F%250Afunction%2520compare(a%252C%2520b)%2520%257B%250A%2520%2520return%2520a%2520%253D%253D%253D%2520b%253B%250A%257D%250A%26input1%26conformanceConfig%26externs%26refasterjs-template%26includeDefaultExterns%3D1%26CHECK_SYMBOLS%3D1%26CHECK_TYPES%3D1%26CLOSURE_PASS%3D1%26LANG_IN_IS_ES6%3D1%26MISSING_PROPERTIES%3D1%26PRESERVE_TYPE_ANNOTATIONS%3D1%26PRETTY_PRINT%3D1%26TRANSPILE%3D1

SLaks commented 8 years ago

On a related note, this error should arguably completely ignore template parameters because of type erasure.