microsoft / microsoft/TypeScript

Function lacks ending return statement and return type does not include 'undefined'

Open
#58,099 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Suggestion
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

https://www.typescriptlang.org/play?jsx=0&ts=5.4.4#code/C4TwDgpgBAyglgL2gXigcgDYEMBOBzCNKAH3QDsB7HAWywyNLQGdaN6T1q4y40AoAMYUyTYFCaIUUAN4BfKFiaxJg4aKgAjDBQEBrKKgA8AJQB8ACgBmALijmAlAdNRjj5M+l8o3qDgjAAVxwyKEsHPlk+VRExASYlVC0dXXMHWzIA6g0IHCcZLx8mAHc4YAEACzsJJEdPH3qoAUVoNEoaOjRbOoaev0DgqABGAp7Inu8mphbsfEIukfHvPqCQgCYF+rHxyZauHk78xfrlgYBaQagNny2enfQWOnp5o58TkIAGK+8bhoATCEsWACGGAzxejTUYjI6QgADccgZxCpFj9IrJ7EA

💻 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

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 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.