Skip to content

Type-asserted variable become unknown again at once change #50440

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
fenying opened this issue Aug 25, 2022 · 1 comment
Closed

Type-asserted variable become unknown again at once change #50440

fenying opened this issue Aug 25, 2022 · 1 comment
Labels
Duplicate An existing issue was already created

Comments

@fenying
Copy link

fenying commented Aug 25, 2022

Bug Report

A type-asserted variable from unknown, would become unknown again at once its value changed

🔎 Search Terms

🕗 Version & Regression Information

  • This problem could be reproduced between versions 4.3.5 (earlier versions not tested) and latest nightly

⏯ Playground Link

Playground link with relevant code

💻 Code

function isNumber(s: unknown): s is number { // it's the same even changed to string or other types

    return typeof s === 'number';
}

let c: unknown = 123;

if (!isNumber(c)) {

    throw new TypeError('what?');
}

c = c + 1;

/**
 * Why would an asserted number become unknown again?
 */
if (c + 1 === 0) {

    console.log(c);
}

🙁 Actual behavior

the assert number-typed variable c became unknown again.

🙂 Expected behavior

It should keep being number type

@MartinJohns
Copy link
Contributor

Duplicate of #27706. Used search terms: unknown narrow in:title

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Aug 25, 2022
@fenying fenying closed this as completed Aug 27, 2022
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