You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Destructuring causes error for null/undefined properties #6784
Ryan Cavanaugh (@RyanCavanaugh) Just to be clear, are you accepting PRs to make { foo: { x } }: Bar (where Bar.foo is optional) an error? Or to add support for { foo?: { x } }: Bar with the emit error (@errorx666) is asking for?
Edit: Never mind, I see from the linked issue that it's the former.
This implies that in cases where I know (because of program context) that the optional property is present in my object, I won't be able to destructure?
Even though that would be valid, working JS?
interfaceX{a?: {b: number}};letx: X;if(x.a){// or some other domain rule that guarantees me 'a' is definedlet{a: { b }}=x;// error: "a" is optional ?}
I don't have any use for such code, just pointing out the possible implications if it becomes an error.
Reacted by Ted, John Munson, Boris Berak and Ahuigo
varproperty=some?.nested?.property?.name;// with is equivallent to:varproperty=some==null?null:some.nested==null?null:some.nested.property==null?null:some.nested.property.name;
in ts this would be current syntax
varsome: {nested?: {property?: {name: string}}}= ...;var{nested: {property: { name }}}=some;// name has type string | undefined
Reacted by Vijay Bindraban, Anthony Proschka, e. b., Josh, George, Sean Wood, Luke Williams, findologic-blauharley, Yohan Nelson, Julian Eden and 4 more
In strict-mode should be invalid. Why only at runtime I get aTypeError: Cannot read property 'toUpperCase' of undefined ?
Thought in strict-mode it is guaranteed that values are not null or undefined implicitly :(((
I think it would be great if this can be fixed,
A proper destructuring should handle all cases recursively like an "empty array" somehow...
Maybe by recursion.
Reacted by Piotr Roszatycki, Kael, Jeffrey Wescott, Kei Ito, Luke Williams, Raul Tambre, Scott Olson, Ruud Andriessen and Tim Moses
@sroze it does Error if you have strict null checks on. When you run in sloppy mode, it behaves as expected and does not error.
The issue is not fixed. When I try to destructure an object in an array, it doesn't throw a compile-time error, but the code explodes at runtime with a TypeError. Even with everything strict
Activity
RyanCavanaugh commentedon Feb 1, 2016
This is just the ES6 destructuring behavior; we don't use type system data to change the runtime behavior.
That said, it's a bad idea to use a property of an optional property as part of a destructuring, and we should probably consider this an error.
RyanCavanaugh commentedon Feb 1, 2016
Accepting PRs. Any property destructuring must be non-optional or have a default
Arnavion commentedon Feb 2, 2016
Ryan Cavanaugh (@RyanCavanaugh) Just to be clear, are you accepting PRs to make
{ foo: { x } }: Bar(where Bar.foo is optional) an error? Or to add support for{ foo?: { x } }: Barwith the emit error (@errorx666) is asking for?Edit: Never mind, I see from the linked issue that it's the former.
falsandtru commentedon Feb 2, 2016
I already said about this problem in #6125. This issue is duplicate.
mhegazy commentedon Feb 6, 2016
we are accepting PRs for flagging destructuring into a optional property as an error. We still do not want to do any type directed emit.
jods4 commentedon Mar 24, 2016
This implies that in cases where I know (because of program context) that the optional property is present in my object, I won't be able to destructure?
Even though that would be valid, working JS?
I don't have any use for such code, just pointing out the possible implications if it becomes an error.
10 remaining items
jods4 commentedon Mar 13, 2017
Andy (Andrewkraft) (@Andy-MS) can we have a syntax to indicate something nullable is actually set and so the error should be ignored? See my previous comment #6784 (comment)
CzBuCHi commentedon Jun 5, 2017
what about doing it same way as c# ?. operator? some random blog with explanation
in ts this would be current syntax
transpiled into:
not sure how to handle multiple nested variables ...
danielbodart commentedon Feb 14, 2018
What's the current status of this?
schmaluk commentedon Apr 21, 2018
In strict-mode should be invalid. Why only at runtime I get aTypeError: Cannot read property 'toUpperCase' of undefined ?
Thought in strict-mode it is guaranteed that values are not null or undefined implicitly :(((
I think it would be great if this can be fixed,
A proper destructuring should handle all cases recursively like an "empty array" somehow...
Maybe by recursion.
sroze commentedon Oct 13, 2019
This code will dramatically fail because destructuring an
undefinedvalue seems to be allowed:Shouldn't TypeScript throw an error if it's the case? Happy to put my hands in TS' code if we have agreement on this expected behaviour :)
kitsonk commentedon Oct 14, 2019
Samuel ROZE (@sroze) it does error if you have strict null checks on. When you run in sloppy mode, it behaves as expected and does not error.
prajun7 commentedon Apr 13, 2022
Ran into the same problem. Any fixes?
RyanCavanaugh commentedon Oct 23, 2025
This works under strictNullChecks, which there's no excuse to not have on at this point
drago1520 commentedon Nov 21, 2025
The issue is not fixed.
The issue is not fixed. When I try to destructure an object in an array, it doesn't throw a compile-time error, but the code explodes at runtime with a TypeError. Even with everything strict
RyanCavanaugh commentedon Nov 24, 2025
drago1520 you're missing
noUncheckedIndexedAccess