title: What is the actual output of console.log(error) and how do I get the exact same output as a string?
body: <p>In a <strong>Node.js</strong> project using Typescript and targeting ES2020, I'm using a custom Error class like this:</p>
  <pre><code>class InvalidParamsError extends Error {
  }
  
  try {
    throw new InvalidParamsError();
  } catch (error) {
    if (error instanceof Error) {
      console.log(error);
      console.log(error.message); // Different output
      console.log(error.stack); // Different output
      console.log(error.toString()); // Different output
    }
  }
  </code></pre>
  <p>The output I get in the console is:</p>
  <pre class="lang-none prettyprint-override"><code>InvalidParamsError
      at REPL11:2:13
      at ...
  
  Error
      at REPL11:2:13
      at ...
  Error
  undefined
  </code></pre>
  <p><strong>How can I get this very same output as a string without having to print it to the console?</strong></p>
  <p><code>error.message</code>, <code>error.stack</code> and <code>error.toString()</code> all return different strings, and none of them show <code>InvalidParamsError</code> as part of the output, showing a generic <code>Error</code> instead. I know I could fix this manually setting the <code>name</code> of the <code>InvalidParamsError</code> in the constructor, but even then I'm unable to get exactly the same output as <code>console.log(error)</code> as a string.</p>
  <p>I tried looking at <a href="https://github.com/nodejs/node/blob/master/lib/internal/console/constructor.js" rel="nofollow noreferrer">Node.js' console source code</a> but couldn't find any answer.</p>
  <p>I'm targeting ES2020 so I don't think this is related with <a href="https://github.com/Microsoft/TypeScript-wiki/blob/main/Breaking-Changes.md#extending-built-ins-like-error-array-and-map-may-no-longer-work" rel="nofollow noreferrer">previous Typescript issues when extending built-ins like Error</a>.</p>