question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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:

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
ortacommented, Jul 17, 2020

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

0reactions
typescript-botcommented, Jan 3, 2023

👋 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

:warning: Assertions:

  • (parameter) x: B

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

:warning: Assertions:

  • (parameter) x: B

</detail>
Read more comments on GitHub >

github_iconTop Results From Across the Web

why does initialization narrow type? · Issue #8513 - GitHub
The idea here, I believe, is that despite the fact that flow analysis uses unsound assumptions and is not really 'perfect' in its...
Read more >
typescript - Why doesn't 'typeof' narrow a union type when the ...
Typescript discriminated unions (currently) only support property values as discriminants, not property types. ; I'm pretty sure your first claim ...
Read more >
Type-Safe TypeScript with Type Narrowing - Rainer Hahnekamp
Whenever we deal with a variable that can be of multiple types, like an unknown or a union type, we can apply type...
Read more >
Documentation - TypeScript 3.7
At its core, optional chaining lets us write code where TypeScript can immediately stop running some expressions if we run into a null...
Read more >
15 Typescript Mistakes To Avoid - SoftwareMill Tech Blog
Writing or skipping types in wrong places · Adding types that can be inferred · Not annotating object literals · Not specifying function...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found