Skip to content
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

Type narrowing with unreachable code error #35239

Closed
Diverclaim opened this issue Nov 20, 2019 · 1 comment
Closed

Type narrowing with unreachable code error #35239

Diverclaim opened this issue Nov 20, 2019 · 1 comment
Labels
Duplicate An existing issue was already created

Comments

@Diverclaim
Copy link

Diverclaim commented Nov 20, 2019

Playground link
playground

Code

function weird() {
  function generateStringOrNumber(): string | number {
    if (Math.random() > 0.5) {
      return 42;
    } else {
      return "blabla";
    }
  }

  return; // this causes problems

  const value = generateStringOrNumber();
  if (typeof value !== "string") {
    const numberValue: number = value;
  }
}

Actual behaviour
Compiler error

Type 'string | number' is not assignable to type 'number'.
  Type 'string' is not assignable to type 'number'.(2322)

Expected behaviour
Should compile fine

Notes
Remove return in function, and it actually compiles fine.

@j-oliveras
Copy link
Contributor

Duplicate of #33674 and #26914?

I only searched narrowing unreachable.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Nov 22, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants