title: In Typescript how do you make a distinction between Node and vanilla Javascript Error types?
body: <p>I have the following function:</p>
  <pre><code>/**
   * Retrieves a component template from filesystem
   */
  const getComponentTemplate = async (
    p: string
  ): Promise&lt;string&gt; =&gt; {
    let template: string
    try {
      template = await fs.readFile(p, {
        encoding: 'utf8'
      })
    } catch (e) {
      if (e instanceof Error &amp;&amp; e.code === 'ENOENT') {
        throw new Error(`template for element type ${elementType} not found`)
      }
      throw e
    }
  
    return template
  }
  </code></pre>
  <p>Typescript complains here:</p>
  <p><code>[ts] Property 'code' does not exist on type 'Error'</code></p>
  <p>This is because the Javascript <code>Error</code> class only has properties <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" rel="nofollow noreferrer">message and name</a>.</p>
  <p>However, Node's <code>Error</code> class does have a <a href="https://nodejs.org/api/errors.html#errorcode" rel="nofollow noreferrer">code property</a>.</p>
  <p>Typescript defines this in a special interface <code>ErrnoException</code> (see source <a href="https://github.com/DefinitelyTyped/DefinitelyTyped/blob/2a1850482040d351929454fef723eedb49a75681/types/node/globals.d.ts#L193-L198" rel="nofollow noreferrer">here</a>). I have added <code>@types/node</code> to my package.json, but this didn't make Typescript realize that this <code>Error</code> is part of the <code>ErrnoException</code> interface.</p>
  <p>It is not possible to declare a type annotation in a catch clause. So, how does one make the Typescript compiler able to resolve that this is a Node Error?</p>
  <p>FYI, this is part of my <code>tsconfig.json</code>:</p>
  <pre><code>{
    &quot;compilerOptions&quot;: {
      &quot;target&quot;: &quot;es2017&quot;,
      &quot;module&quot;: &quot;commonjs&quot;,
      &quot;lib&quot;: [&quot;es2017&quot;]
      ...
    }
  }
  </code></pre>