Skip to content

Does not completely narrow type through null coalesce operator in switch #944

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
jkieboom opened this issue May 26, 2025 · 0 comments · Fixed by #959
Closed

Does not completely narrow type through null coalesce operator in switch #944

jkieboom opened this issue May 26, 2025 · 0 comments · Fixed by #959
Assignees
Labels
bug Something isn't working

Comments

@jkieboom
Copy link

In the following example regular typescript narrows the type of the value in the switch statement to "never" when having checked all cases exhaustively:

interface ClientSource {
  type: "client";
}

interface ServiceSource {
  type: "service";
}

type Source = ClientSource | ServiceSource;

function isDisplaySource(source: Source | null | undefined): boolean {
  switch (source?.type) {
    case "client":
      return true;
    case "service":
      return false;
    case undefined:
      return false;
    default:
      neverReached(source);
      return false;
  }
}

function neverReached(_v: never): void {}

With tsgo however I get the following error:

src/not-reached-narrow.ts:20:20 - error TS2345: Argument of type 'null | undefined' is not assignable to parameter of type 'never'.
  Type 'undefined' is not assignable to type 'never'.

20       neverReached(source);
@ahejlsberg ahejlsberg added the bug Something isn't working label May 27, 2025
@ahejlsberg ahejlsberg self-assigned this May 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants