You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
If a union has undefined an initializer will remove null or void from the union as well. #14406
TypeScript Version: locally with 2.2.1 and in playground
Code
with strictNullChecks option in playground
type Opaque = {} | null | void | undefined;
class VersionOne {
x: Opaque;
constructor(x: {} | null | void = null) {
this.x = x;
}
}
new VersionOne(<Opaque>2);
class VersionTwo {
constructor(public x: {} | null | void = null) {
}
}
new VersionTwo(<Opaque>1);
class VersionThree {
constructor(public x: Opaque = null) { // null is not assignable to {}
}
}
new VersionThree(<Opaque>1); // null is not assignable to {} | undefined
class VersionFour {
constructor(public x: number | void | undefined = 1) {
}
};
let y = new VersionFour().x + 5; // should error but does not
class VersionFive {
constructor(public x: number | void = 1) {
}
};
y = new VersionFive().x + 3; // plus cannot be applied to number | void
Expected behavior:
if undefined is specified explicitly by a union the initializer should not remove the void or null along with undefined.
I would expect VersionThree to behave like VersionTwo and VersionFour to be like VersionFive
Actual behavior:
adding undefined to the type union causes the resulting type to lose null or void.
Activity
chancancode commentedon Mar 2, 2017
This is a regression somewhere between TypeScript 2.1 and 2.2 – the last known good version was 2.1.6
DanielRosenwasser commentedon Mar 9, 2017
Can you guys take a look at our latest nightly to see if #14498 has fixed the issue?
krisselden commentedon Mar 9, 2017
Daniel Rosenwasser (@DanielRosenwasser) I pretty much can tell by the PR that it will fix this issue, though
voidwasn't checked as well.krisselden commentedon Jul 14, 2017
This was fixed by #14498