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

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

Open
ShuiRuTian opened this issue Jun 17, 2020 · 7 comments
Labels
Bug A bug in TypeScript Has Repro This issue has compiler-backed repros: https://aka.ms/ts-repros
Milestone

Comments

@ShuiRuTian
Copy link
Contributor

ShuiRuTian commented Jun 17, 2020

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:

@ShuiRuTian
Copy link
Contributor Author

ShuiRuTian commented Jun 17, 2020

The reason is isMatchingReferenceDiscriminant function return false at the second time. Declared type is no use here.

#38839 Add this link, for I meet similar problem in the fix.

@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Jun 17, 2020
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Jun 17, 2020
@typescript-bot typescript-bot added the Has Repro This issue has compiler-backed repros: https://aka.ms/ts-repros label Jul 17, 2020
@orta
Copy link
Contributor

orta commented Jul 17, 2020

( I sneakily edited the issue body to be a twoslash compiler repro to test the bot )

@orta

This comment has been minimized.

@typescript-bot

This comment has been minimized.

@orta
Copy link
Contributor

orta commented Jul 17, 2020

Think I've got the bugs down, @typescript-bot run repros

@typescript-bot
Copy link
Collaborator

Heya @orta, I've started to run the code sample repros for you. Here's the link to my best guess at the log.

@typescript-bot
Copy link
Collaborator

typescript-bot commented Apr 13, 2022

👋 Hi, I'm the Repro bot. I can help narrow down and track compiler bugs across releases! This comment reflects the current state of the repro in the issue body running against the nightly TypeScript.


Issue body code block by @ShuiRuTian

⚠️ Assertions:

  • (parameter) x: B

Historical Information
Version Reproduction Outputs
4.2.2, 4.3.2, 4.4.2, 4.5.2, 4.6.2

⚠️ Assertions:

  • (parameter) x: B

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Has Repro This issue has compiler-backed repros: https://aka.ms/ts-repros
Projects
None yet
Development

No branches or pull requests

4 participants