Skip to content

If a union has undefined an initializer will remove null or void from the union as well. #14406

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

Closed
krisselden opened this issue Mar 2, 2017 · 4 comments
Labels
Needs Investigation This issue needs a team member to investigate its status.

Comments

@krisselden
Copy link

krisselden commented Mar 2, 2017

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.

@chancancode
Copy link
Contributor

This is a regression somewhere between TypeScript 2.1 and 2.2 – the last known good version was 2.1.6

@DanielRosenwasser
Copy link
Member

Can you guys take a look at our latest nightly to see if #14498 has fixed the issue?

@krisselden
Copy link
Author

@DanielRosenwasser I pretty much can tell by the PR that it will fix this issue, though void wasn't checked as well.

@RyanCavanaugh RyanCavanaugh added the Needs Investigation This issue needs a team member to investigate its status. label May 24, 2017
@krisselden
Copy link
Author

This was fixed by #14498

@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Needs Investigation This issue needs a team member to investigate its status.
Projects
None yet
Development

No branches or pull requests

4 participants