Skip to content

Commit 4edba9b

Browse files
Closure Teamcopybara-github
authored andcommitted
Don't register types mismatch if actual type satisfies any of required union's alternates
PiperOrigin-RevId: 512008241
1 parent c783340 commit 4edba9b

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

src/com/google/javascript/jscomp/TypeMismatch.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,13 @@ void registerMismatch(Node location, JSType found, JSType required) {
7070
if (found.isSubtypeOf(required) || required.isSubtypeOf(found)) {
7171
return;
7272
}
73+
if (required.isUnionType()) {
74+
for (JSType requiredAltType : required.toMaybeUnionType().getAlternates()) {
75+
if (requiredAltType.isSubtypeOf(found)) {
76+
return;
77+
}
78+
}
79+
}
7380

7481
if (bothAreNotTemplateTypes(found, required)) {
7582
this.mismatches.add(TypeMismatch.create(found, required, location));

test/com/google/javascript/jscomp/TypeValidatorTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,16 @@ public void testSubclass() {
334334
this.assertThatRecordedMismatches().isEmpty();
335335
}
336336

337+
@Test
338+
public void testUnionsMismatch() {
339+
testWarning(
340+
"/** @param {number|string} x */\n"
341+
+ "function f(x) {}\n"
342+
+ "f(/** @type {boolean|string} */ ('a'));",
343+
TYPE_MISMATCH_WARNING);
344+
this.assertThatRecordedMismatches().isEmpty();
345+
}
346+
337347
@Test
338348
public void testModuloNullUndef1() {
339349
testSame(

0 commit comments

Comments
 (0)