title: Right way to reference platform-specific types in isomorphic Typescript library
body: <p>I'm trying to write a Typescript library that I'd like to be able to include when targeting both the browser and Node.  I have two problems: referring to platform-specific types in the body of the code, and the inclusion of those types in the generated <code>.d.ts</code> declarations that accompany the transpiled JS.</p>
  <p>In the first case, I want to write something like</p>
  <pre><code>  if (typeof window === &quot;undefined&quot;) {
      // Do some Node-y fallback thing
    } else {
      // Do something with `window`
    }
  </code></pre>
  <p>This fails to compile if I don't include <code>&quot;dom&quot;</code> in my <code>lib</code> compiler option (that is, if I just say <code>lib: [&quot;es2016&quot;]</code> in <code>tsconfig</code>), because the global <code>window</code> is not defined.  (Using <code>window</code> is just an example of something out of <code>lib.dom.d.ts</code>, it may also be <code>fetch</code> or a <code>Response</code> or <code>Blob</code>, etc.)  The point is that the code should already be safe at runtime by checking for the existence of the global object before using it, it's the type side that I can't figure out.</p>
  <p>In the second case, I'm getting an error trying to include the library after it builds.  I can build the library using <code>&quot;dom&quot;</code> in the <code>lib</code> option, and the resulting output includes typings with e.g. <code>declare export function foo(x: string | Blob): void</code>.  The problem is, if the consuming code doesn't include a definition for <code>Blob</code> (no <code>&quot;dom&quot;</code> lib), it fails to compile, even though it's only actually calling <code>foo</code> with a <code>string</code> (or not using <code>foo</code> at all!).</p>
  <p>I don't want my library (or the consumer) to try to pollute the global namespace with fake <code>window</code> or <code>Blob</code> declarations if I can help it.  More isometric libraries have been popping up but I haven't found a good Typescript example to follow.  (If it's too complex a topic for SO, I'd still greatly appreciate a pointer to documentation or an article/blog post.)</p>