microsoft / microsoft/TypeScript
improper declared type cause narrow type not work when its computed type is not union.
Open
Nobody has claimed this yet.
Bug
Domain: check: Control Flow
Has Repro
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
Description
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.
Related Issues:
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the linked TypeScript Playground repro using TypeScript 4.0, and inspect the compiler's control-flow type narrowing behavior after the two discriminant checks. Done means the final x is narrowed to never, matching the expected behavior, while the existing A/B narrowing remains correct.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Clearly specified
- Newbie friendliness
- 55/100