Microsoft/TypeScript
GitHub ã§èŠãswitch (typeof something) isn't seen as exhaustive
Open
#56,786 opened on 2023幎12æ14æ¥
Domain: check: Control FlowHelp WantedPossible Improvement
Repository metrics
- Stars
- Â (48,455 stars)
- PR merge metrics
-  (å¹³åããŒãž 6d 17h) (30d ã§ 9 merged PRs)
説æ
ð Search Terms
- typeof
- switch
- narrow
- type guard
ð Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about Type Guards
⯠Playground Link
ð» Code
function checkAll<T>(something: T): T {
switch (typeof something) {
case "number":
case "bigint":
case "boolean":
case "symbol":
case "undefined":
case "function":
case "object":
case "string":
return something;
}
}
ð Actual behavior
This emits the TS error: "Function lacks ending return statement and return type does not include 'undefined'.(2366)"
ð Expected behavior
No error, since all possible results of typeof are handled.
Additional information about the issue
It seems to only be an issue when the function argument is generic. It works if I switch the return type and parameter types to any, but I'm not sure that's a fair comparison.