Open bengry opened 1 month ago
destructuring, Cannot destructure as it is undefined, optional chaining destructuring
destructuring
Cannot destructure as it is undefined
optional chaining destructuring
https://www.typescriptlang.org/play/?ts=5.6.2#code/MYewdgzgLgBAlmADgVygLhgbwLACgYEwCGA-BpjAEZlYzAbQBOCA5jAL4d6cC8tRGZGAAmAUwBmCUcK648oSLAoA6VZQ4w+CFFGWlllIA
const input: { a?: { b?: { c: string } } } = { a: undefined } const { ...b } = input.a?.b
No error reported by TypeScript.
TypeScript should have an indicative error, since this expression might result in undefined, which cannot be destructured.
undefined
Similar to this:
Rest types may only be created from object types.(2700)
The error in the JavaScript being ran is:
Uncaught TypeError: Cannot destructure '(intermediate value)' as it is undefined.
@Andarist Does your PR #60121 fix this as well?
Yes :) https://github.com/microsoft/TypeScript/pull/60121/commits/c0ef60948eb1910d923cb26252d2f4781adcd284
🔎 Search Terms
destructuring
,Cannot destructure as it is undefined
,optional chaining destructuring
🕗 Version & Regression Information
⏯ Playground Link
https://www.typescriptlang.org/play/?ts=5.6.2#code/MYewdgzgLgBAlmADgVygLhgbwLACgYEwCGA-BpjAEZlYzAbQBOCA5jAL4d6cC8tRGZGAAmAUwBmCUcK648oSLAoA6VZQ4w+CFFGWlllIA
💻 Code
🙁 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:
Additional information about the issue
The error in the JavaScript being ran is: