Content-Length: 296755 | pFad | https://github.com/microsoft/TypeScript/issues/39114

CF improper declared type cause narrow type not work when its computed type is not union. · Issue #39114 · microsoft/TypeScript · GitHub
Skip to content

improper declared type cause narrow type not work when its computed type is not union. #39114

@ShuiRuTian

Description

@ShuiRuTian

TypeScript Version: 4.0

Search Terms:

Code

interface A {
    type:'A'
}

interface B {
    type:'B'
}

function assert(x:any) :asserts x is A|B{

}

function foo(x: any): any {
    x;  // any
    assert(x);
    x;  // A | B
    if (x.type === 'A') {
        return x;
    }
    x;  // B
    if (x.type === 'B') {
        return x;
    }
    x;  // B, but should be never.
//  ^?
}

Expected behavior:
the last x should be never.

Actual behavior:
still B.

Playground Link:
https://www.typescriptlang.org/play/index.html?ts=4.0.0-dev.20200615&ssl=1&ssc=1&pln=25&pc=2#code/JYOwLgpgTgZghgYwgAgIIG8BQyfLATwAcIAuAclTMwF9NNRJZEUAhLXPI0slq2zGAFcQCMMAD2IZHADOM6GAAUADxJwQ+AJTI1chTOTLkwA6gA+bOvyEixk5DHHiVJaRs2v1+ZO1zKA3DgA9EFu+Ni4svJQSsqa-hE4AcGhqMhmyCyJxjDIKgB0BMTIALxlyBRk2r4cOFAQYIJQUgHZtBzJyCGZ2cC5BUUoZSUVvNXZHPWNzYYJHO1+gV2hWdRAA

Related Issues:

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptHas ReproThis issue has compiler-backed repros: https://aka.ms/ts-repros

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions









      ApplySandwichStrip

      pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


      --- a PPN by Garber Painting Akron. With Image Size Reduction included!

      Fetched URL: https://github.com/microsoft/TypeScript/issues/39114

      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy