Skip to content

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

Closed
@fenying

Description

@fenying

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions