improper declared type cause narrow type not work when its computed type is not union.
See original GitHub issueTypeScript 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.
Related Issues:
Issue Analytics
- State:
- Created 3 years ago
- Comments:7 (4 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
( I sneakily edited the issue body to be a twoslash compiler repro to test the bot )
👋 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
:warning: Assertions:
(parameter) x: B