Skip to content

When a unknown variable is narrowed to a specific type, assigning another value with the same type resets the first back to unknown #42687

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
AlCalzone opened this issue Feb 7, 2021 · 2 comments

Comments

@AlCalzone
Copy link
Contributor

AlCalzone commented Feb 7, 2021

Bug Report

🔎 Search Terms

unknown, number, assign

🕗 Version & Regression Information

On the playground this behavior goes back to at least 3.3.3333

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about unknown
  • I was unable to test this on prior versions because there is no earlier version on the playground

⏯ Playground Link

Playground link with relevant code

💻 Code

declare function takesNumber(value: number): void;

function test(value: unknown) {
  if (typeof value !== "number") return;
  takesNumber(value); // ok
  value = 1; // assign the same type!
  takesNumber(value); // error (value is unknown, although we narrowed it to number before)
}

🙁 Actual behavior

value is unknown after the assignment

🙂 Expected behavior

value should still be number

@AlCalzone AlCalzone changed the title Comparison with enum loses type-narrowing from unknown to number When a unknown variable is narrowed to a specific type, assigning another value with the same type resets the first back to unknown Feb 7, 2021
@whzx5byb
Copy link

whzx5byb commented Feb 7, 2021

Related: #27706 (comment), #16976 (comment)

@AlCalzone
Copy link
Contributor Author

Huh, seems like this is an exact duplicate of #27706. Man, unknown is really still awkward.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants