Skip to content

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

Closed

Description

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

  1. chancancode commented on Mar 2, 2017

    @chancancode
    Contributor

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

  2. DanielRosenwasser commented on Mar 9, 2017

    @DanielRosenwasser
    Member

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

  3. krisselden commented on Mar 9, 2017

    @krisselden
    Author

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

  4. added
    Needs Investigation
    on May 24, 2017
  5. krisselden commented on Jul 14, 2017

    @krisselden
    Author

    This was fixed by #14498

  6. locked and limited conversation to collaborators on Jun 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs Investigation

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions