microsoft / microsoft/TypeScript

narrowing in switch doesn't work with non-union types

Open
#16,976 13 comments 9 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

In Discussion Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

class A {
    readonly kind = 'a';
}
type U = A; // <-- supposed to be a single case union
declare var u: U;
declare function never(never: never): never;
function fn() {
    switch (u.kind) {
        case 'a': return 1;
        default: return never(u); // <-- u expected to be never, actually is A
    }
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the provided TypeScript reproduction and inspect the type-checking path for switch-based narrowing and exhaustiveness. Confirm whether the default branch accepts u as A rather than never, then add or update a regression test showing that a single-case union is narrowed correctly.

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
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.