microsoft / microsoft/TypeScript
Type parameter constrained to union cannot be exhaustively narrowed to 'never'
Open
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
TypeScript Version: 2.1.1 (http://www.typescriptlang.org/play/index.html)
Code
type Type = "a" | "b"
function isA(a: any): a is "a" {
return a === "a"
}
function isB(a: any): a is "b" {
return a === "b"
}
function assertNever(arg: never) {
throw new Error("This should never be called")
}
function handleAction<T extends Type>(type: T) {
if (isA(type)) {
return type
} else if (isB(type)) {
return type
} else {
assertNever(type)
}
}
Expected behavior:
No compilation error
Actual behavior:
Argument of type 'T' is not assignable to type 'never'
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 by reproducing the provided TypeScript 2.1.1 example and verify the error at assertNever(type). Investigate generic control-flow narrowing for T extends a string-literal union; done means the example compiles without treating the exhaustive else branch as an error.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 25/100