Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Destructuring causes error for null/undefined properties

See original GitHub issue

The following code:

interface Foo {
    x: number;

interface Bar {
    foo?: Foo;

function test( { foo: { x } }: Bar ) {
    alert( x );

test( {} ); // TypeError: Cannot read property 'x' of undefined

transpiles to:

function test(_a) {
    var x =;

instead of the (arguably) preferable:

function test(_a) {
    var x;
    if ( x =;

The transpiled code does not check for the presence of optional property foo in the Bar interface. This leads to an error at invocation.

I can see three possible solutions:

  1. Always check for member existence when transpiling destructuring statements.
  2. Check for member existence only when an element is optional in the interface or type declaration.
  3. New syntax for destructuring that can indicate members should be checked.


function test( { foo?: { x } }: Bar ) {}

Issue Analytics

  • State:open
  • Created 8 years ago
  • Reactions:7
  • Comments:22 (10 by maintainers)

github_iconTop GitHub Comments

danielbodartcommented, Feb 14, 2018

What’s the current status of this?

pauldijoucommented, Nov 24, 2016

I think you also have the problem when destructuring arrays.

const [a, b, c] = ''.split('')

TypeScript will consider all three variables to be string while they should be string | undefined (because, they are totally undefined here)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Prevent Error with Default {} when Destructuring
You have to check for TypeError and then make sure it's a TypeError around reading a property of undefined or null. That way...
Read more >
Cannot destructure Property of Null Error in JavaScript
The "Cannot destructure property of null" error occurs when we try to destructure a property from a null value. To solve the error,...
Read more >
JS/ES6: Destructuring of undefined - javascript - Stack Overflow
Note: The default value would only be applied if the destructured param is undefined , which means that destructuring null values will throw...
Read more >
TIL: Destructuring from an Object with a possibly Undefined ...
Destructure from a possibly undefined object property. Problem ... undefined (not good, we needed this to be null)// Result: FAILURE.
Read more >
Optional chaining (?.) - JavaScript - MDN Web Docs - Mozilla
The ?. operator is like the . chaining operator, except that instead of causing an error if a reference is nullish ( null...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Post

No results found

github_iconTop Related Hashnode Post

No results found