microsoft / microsoft/TypeScript
Function lacks ending return statement and return type does not include 'undefined'
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 117
Description
🔎 Search Terms
switch default
🕗 Version & Regression Information
The tsc complains that the the function return undefined if a add a default case, actually the default case will never be executed, just only for exhausive check.
⏯ Playground Link
💻 Code
type Size = 'large' | 'normal' | 'small' | 'mini'
const size = {} as Size
const block = <R>(f: () => R) => {
return f()
}
const css = block((): number => {
switch (size) {
case 'normal': {
return 1
}
case 'large': {
return 2
}
case 'mini': {
return -1
}
case 'small': {
return 0
}
default: {
const n: never = size
}
}
})
🙁 Actual behavior
The tsc think that the function of css block returns number | undefined.
🙂 Expected behavior
The tsc should know that the default case is never returned, and the function of css block should returns number
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 linked TypeScript Playground reproduction and the switch statement in the issue body. Trace how the type checker handles the exhaustive default branch and the enclosing arrow function's return analysis. Done means the example's inferred return type is number rather than number | undefined, with regression coverage for this behavior.
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
- Clearly specified
- Newbie friendliness
- 35/100