2

I'm trying to extract all properties from an Error instance by doing.

const test = new Error('hi there');
console.log({...test});  // empty object (I expected name + message)
console.log(test.message);  // "hi there"
console.log(test.hasOwnProperty("message"));  // True

Why is it not possible to destructure the message property from the Error instance in a new object?

Evers
  • 1,858
  • 1
  • 17
  • 18
  • 2
    Does this answer your question? [What is the reason for not spread (es 6 spread operator) javascript Error object](https://stackoverflow.com/questions/53167026/what-is-the-reason-for-not-spread-es-6-spread-operator-javascript-error-object) – Jaye Renzo Montejo Jul 17 '20 at 13:16

1 Answers1

0

The properties of an Error instance are not enumerable, which means they won't participate in the spread syntax extraction, or in for ... in etc.

Pointy
  • 405,095
  • 59
  • 585
  • 614