Microsoft/TypeScript
GitHub ã§èŠãDestructuring a possibly `undefined` object not caught, throws an error at runtime
Open
#60,179 opened on 2024幎10æ9æ¥
BugDomain: check: Control FlowHelp Wanted
Repository metrics
- Stars
- Â (48,455 stars)
- PR merge metrics
-  (å¹³åããŒãž 6d 17h) (30d ã§ 9 merged PRs)
説æ
ð Search Terms
destructuring, Cannot destructure as it is undefined, optional chaining destructuring
ð Version & Regression Information
- This changed between versions ______ and _______
- This changed in commit or PR _______
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about _________
- I was unable to test this on prior versions because _______
⯠Playground Link
ð» Code
const input: {
a?: { b?: { c: string } }
} = { a: undefined }
const { ...b } = input.a?.b
ð Actual behavior
No error reported by TypeScript.
ð Expected behavior
TypeScript should have an indicative error, since this expression might result in undefined, which cannot be destructured.
Similar to this:
Rest types may only be created from object types.(2700)
Additional information about the issue
The error in the JavaScript being ran is:
Uncaught TypeError: Cannot destructure '(intermediate value)' as it is undefined.