I was a huge fan of destructuring as it gives me a more concise looking code. But when things get big and many objects are destructured, everything is just losing its context. Take this example
const{prop}=objconstmyObj=obj// You might have to check to which object this prop belongs to, or even you might not realize if it's a property of an object.console.log(prop)// You know immediately that prop belongs to myObjconsole.log(myObj.prop)
Not mentioning if you have a colliding property names then you have to make an alias for the colliding property name so you would still ended up in a messy code
When using the spread syntax to copy objects that have nested properties, it is crucial to understand how JavaScript handles references to these nested properties. The spread operator creates a shallow copy of the object. This means that while the top-level properties are copied, any nested objects or arrays are still referenced from the original object. Consequently, changes to nested properties in the copied object will also reflect in the original object and vice versa.
Here, we define an object person with properties like name, age and address. And address object contains city and zip.
Shallow Copy with Spread Operator
constpersonCopy={...person};
The spread operator { ...person } creates a shallow copy of person. This means that the top-level properties (name, age, and address) are copied directly into personCopy. However, since address is an object (a nested property), only the reference to this object is copied, not the actual nested object itself.
Since personCopy.address is a reference to the same address object as person.address, changing personCopy.address.city to 'Los Angeles' affects both person and personCopy. Hence, both console.log statements output "Los Angeles".
Modifying Nested Property Again
person.address.city='New York';console.log(person.address.city);// "New York"console.log(personCopy.address.city);// "New York"
Similarly, when we change person.address.city back to 'New York', it also affects personCopy.address.city because they reference the same address object. As a result, both console.log statements output "New York".
How does JavaScript destructuring interact with TypeScript types and interfaces — what are best practices for typing destructured variables, function parameters, and defaults to avoid runtime/compile-time mismatches?
TypeScript treats destructuring as a structural extraction pattern, not a type transformation. The type system applies to the source value, and TS infers the types of the destructured bindings from it. A mismatch can happen when you try to conflate JS runtime defaults with TS compile-time optionality, or when you type the destructuring pattern instead of the source.
In summary -
Scenario
Best Practice
Why
Missing properties at runtime
Validate before destructuring (Zod, io-ts, or manual checks)
TS erases at runtime; const { a } = null crashes
Overly broad types
Use satisfies to validate structure while preserving exact types
Prevents type widening without losing literals
Union types in params
Narrow first, then destructure
TS only allows common properties across union branches
Rest destructuring
Type the source, let TS infer rest, or use Omit
Avoids index signature pollution
API boundaries
Define explicit interfaces, never inline object types
Enables reuse, documentation, and contract testing
Lastly a quick reference checklist -
Type the source object/array, not the destructuring pattern,
Mark properties ? in types if you rely on JS defaults,
Default the entire parameter object (= {}) if callers might omit it,
Use satisfies for structure validation without type widening,
Validate untrusted input before destructuring,
Narrow unions before extracting branch-specific properties,
Use Omit/Pick to type ...rest destructuring safely.
I build tools that make developers faster and help startups grow. I specialize in building agentic workflows, oss infrastructure, high-performance web systems, and creating technical content.
Thank you for your detailed feedback! Pleased to know that you found the guide comprehensive and the examples clear. Looking forward to bringing you more useful content!
And hey! Thanks again 🙃 for your support!
Thank you so much! 😊
Writing is incredibly rewarding, and I'd love to see what you come up with. Let me know if you need any suggestions on getting started, don't hesitate to reach out!
Great article!!
Would love to know the performance overhead of destructuring. As js devs, we don't really care abt minor performance issue, but still it would be interesting to know.
Thank you for your kind words. You asked so I did a quick research on this topic.
I didn't actually thought about the performance consideration before. Well, it is indeed a critical point to think about.
Well as I far as I realised, Destructuring in JavaScript does introduce a slight performance overhead compared to traditional property access (dot notation) due to the extra steps involved in creating and assigning variables. In scenarios where performance is critical, this overhead can become noticeable.
For instance:
Server Request Handling: When handling a large number of simultaneous requests, every microsecond counts. Extensively using destructuring in request processing functions can introduce slight delays.
WebSocket Data Processing: In a WebSocket server processing high-frequency messages, avoiding destructuring might help reduce overhead.
Game Development Loop: In game development, the render loop runs many times per second. Using destructuring could add unnecessary overhead.
// Traditional property accessfunctionupdateGameState(state){constx=state.player.x;consty=state.player.y;// update game state}// DestructuringfunctionupdateGameState(state){const{x,y}=state.player;// update game state}
Real-time Data Processing: In applications like live data feeds or stock tickers, every bit of performance optimization helps.
There could be more examples. But I think these are enough.
But for most practical purposes, the performance difference is negligible and should not deter you from using destructuring.
As you have been developing applications in JavaScript long before I started, you might know all of this better than I do.
I was a huge fan of destructuring as it gives me a more concise looking code. But when things get big and many objects are destructured, everything is just losing its context. Take this example
Not mentioning if you have a colliding property names then you have to make an alias for the colliding property name so you would still ended up in a messy code
Understood! I shall keep this in mind.
When using the
spreadsyntax to copy objects that havenested properties, it is crucial to understand how JavaScript handlesreferencesto thesenested properties. Thespread operatorcreates ashallow copyof the object. This means that while the top-level properties are copied, any nested objects or arrays are stillreferencedfrom the original object. Consequently, changes tonested propertiesin thecopied objectwill also reflect in theoriginal objectand vice versa.Let's take an example:
Here, we define an object
personwith properties likename,ageandaddress. Andaddressobject containscityandzip.Shallow Copy with Spread Operator
The spread operator
{ ...person }creates a shallow copy ofperson. This means that the top-level properties(name, age, and address)are copied directly intopersonCopy. However, sinceaddressis anobject(a nested property), only thereferenceto this object is copied, not theactual nested object itself.Modifying Nested Property
Since
personCopy.addressis areferenceto the sameaddressobject asperson.address, changingpersonCopy.address.cityto'Los Angeles'affects bothpersonandpersonCopy. Hence, bothconsole.logstatements output"Los Angeles".Modifying Nested Property Again
Similarly, when we change
person.address.cityback to'New York', it also affectspersonCopy.address.citybecause theyreferencethe sameaddressobject. As a result, bothconsole.logstatements output"New York".Massive! That's a brief write up! I learnt something new.
Thank you 😊!
How does JavaScript destructuring interact with TypeScript types and interfaces — what are best practices for typing destructured variables, function parameters, and defaults to avoid runtime/compile-time mismatches?
TypeScript treats destructuring as a structural extraction pattern, not a type transformation. The type system applies to the source value, and TS infers the types of the destructured bindings from it. A mismatch can happen when you try to conflate JS runtime defaults with TS compile-time optionality, or when you type the destructuring pattern instead of the source.
In summary -
Lastly a quick reference checklist -
?in types if you rely on JS defaults,(= {})if callers might omit it,...restdestructuring safely.Thank you very much!!
Glad I could be helpful!
Really well-explained!!
Thanks a lot for your positive feedback! I’m always looking to improve.
Thank you for your detailed feedback! Pleased to know that you found the guide comprehensive and the examples clear. Looking forward to bringing you more useful content!
And hey!
Thanks again 🙃 for your support!
awesome :)
Thank u for the appreciation :)
Well explained... It was really helpful for me
Thank you! It’s great to hear that you've found the explanation helpful!
This is amazing, I should also consider writing.
Thank you so much! 😊
Writing is incredibly rewarding, and I'd love to see what you come up with. Let me know if you need any suggestions on getting started, don't hesitate to reach out!
I'd love to read what you write—go for it!
Great! Loved it
Great article!!
Would love to know the performance overhead of destructuring. As js devs, we don't really care abt minor performance issue, but still it would be interesting to know.
Thank you for your kind words. You asked so I did a quick research on this topic.
I didn't actually thought about the performance consideration before. Well, it is indeed a critical point to think about.
Well as I far as I realised, Destructuring in JavaScript does introduce a slight performance overhead compared to traditional property access (dot notation) due to the extra steps involved in creating and assigning variables. In scenarios where performance is critical, this overhead can become noticeable.
For instance:
There could be more examples. But I think these are enough.
But for most practical purposes, the performance difference is negligible and should not deter you from using destructuring.
As you have been developing applications in JavaScript long before I started, you might know all of this better than I do.
All the lightbulbs went off above my head when you tied destructing and imports. Thank you!!!!
😊 🙌 😊 🙌