title: How to declare global NodeJS variables within my ambient declaration file?
body: <p>This is a follow-up question to <a href="https://stackoverflow.com/q/42317942/40480">this one</a>.</p>
  
  <p>Given the answer on that question, let's say I have the following ambient declaration file:</p>
  
  <pre><code>declare namespace Twilio {
      interface IDevice {
          ready(handler: Function): void;
      }
  
      let Device: IDevice;
  }
  </code></pre>
  
  <p>This is working nicely on my application <code>.ts</code> files, <code>Twilio.Device.ready</code> is fully recognized. But I have my unit tests running with Jest and AFAIK, they run on a NodeJS environment.</p>
  
  <p>As an over simplified mock on my tests I have this (which are <code>.ts</code> files too):</p>
  
  <pre><code>global.Twilio = {
       Device: {
           ready: () =&gt; {},
           offline: () =&gt; {},
           error: () =&gt; {},
       }
  };
  </code></pre>
  
  <p>But this <code>Twilio</code> instance is not recognized. I can fix that by adding something like below to a <code>.d.ts</code> file:</p>
  
  <pre><code>declare global {
      namespace NodeJS {
          interface Global {
              Twilio: any;
          }
      }
  }
  
  export = {}; // This is needed otherwise `declare global` won't work
  </code></pre>
  
  <p>However, I can't have this piece of code on the same file as the <code>declare namespace Twilio</code> I first mentioned. They need to be on separate files, otherwise <code>global.Twilio</code> on my tests will be recognized but <code>Twilio.Device</code> on my code won't.</p>
  
  <p>So my question is, how can I get both instances of <code>Twilio</code> to be recognized in the app code and in the tests code?</p>
  
  <p>And as a bonus question, it would be nice if I could use the <code>Twilio</code> namespace declaration, somehow, as the type of the NodeJS <code>Twilio</code> global instead of <code>any</code>.</p>
  
  <h1><strong>EDIT:</strong></h1>
  
  <p>After a nice chat with Richard Seviora, discussing all my issues, I ended up with the following <code>twilio.d.ts</code> file for my project:</p>
  
  <pre><code>/**
   * Namespace declaration for the Twilio.js library global variable.
   */
  declare namespace Twilio {
  
      type DeviceCallback = (device : IDevice) =&gt; void;
      type ConnectionCallback = (connection : IConnection) =&gt; void;
      type ErrorCallback = (error : IError) =&gt; void;
  
      interface IConnection {
          parameters: IConnectionParameters;
      }
  
      interface IConnectionParameters {
          From?: string;
          To?: string;
      }
  
      interface IDevice {
          cancel(handler: ConnectionCallback): void;
          connect(params: IConnectionParameters): IConnection;
          disconnect(handler: ConnectionCallback): void;
          error(handler: ErrorCallback): void;
          incoming(handler: ConnectionCallback): void;
          offline(handler: DeviceCallback): void;
          ready(handler: DeviceCallback): void;
          setup(token: string, params: ISetupParameters): void;
      }
  
      interface IError {
          message?: string;
          code?: number;
          connection?: IConnection;
      }
  
      interface ISetupParameters {
          closeProtection: boolean;
      }
  
      let Device: IDevice;
  
  }
  
  /**
   * Augment the Node.js namespace with a global declaration for the Twilio.js library.
   */
  declare namespace NodeJS {
  
      interface Global {
          Twilio: {
              // Partial type intended for test execution only!
              Device: Partial&lt;Twilio.IDevice&gt;;
          };
      }
  
  }
  </code></pre>
  
  <p>Hopefully others find this question and Richard's answer below insightful (since the declarations documentation is kinda lacking).</p>
  
  <p>Thanks again Richard for all your assistance.</p>