microsoft / microsoft/TypeScript
Discriminating property with never as possible type makes its enclosing object type disappear when narrowing
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
Description
🔎 Search Terms
discriminated union never, discriminating property never
🕗 Version & Regression Information
It happens in every version I tried.
⏯ Playground Link
💻 Code
type DiscUnion =
| { discProp: never, a: 1 }
| { discProp: "prop", b: 2 }
| { discProp: undefined, c: 3 }
declare const o: DiscUnion
if(o.discProp) {
o
// ^? { discProp: "prop"; b: 2; }
} else {
o
// ^? { discProp: undefined; c: 3; }
}
if(!o.discProp) {
o
// ^? { discProp: undefined; c: 3; }
} else {
o
// ^? { discProp: "prop"; b: 2; }
}
🙁 Actual behavior
{ discProp: never, a: 1 } disappears!
🙂 Expected behavior
In the first if, I expect { discProp: never, a: 1 } to be a possible type of o in the else branch. Viceversa, in the second if I expect it to be a possible type of o in the then branch.
Additional information about the issue
No response
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 supplied TypeScript Playground reproduction and run the DiscUnion example as written. Trace narrowing for both if(o.discProp) and if(!o.discProp); the work is done when the { discProp: never, a: 1 } member remains possible in the expected branches without changing the other narrowed members.
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
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100