title: How to define global function in TypeScript?
body: <p>I want to define a global function that is available everywhere, without the need to import the module when used.</p>
  
  <p>This function aims to replace the safe navigation operator (?) available in C#. For the sake of readability, I don't want to prefix the function with a module name.</p>
  
  <p>Global.d.ts:</p>
  
  <pre><code>declare function s&lt;T&gt;(someObject: T | null | undefined, defaultValue?: T | null | undefined) : T;
  </code></pre>
  
  <p>Global.tsx:</p>
  
  <pre><code>///&lt;reference path="Global.d.ts" /&gt;
  
  export function s&lt;T&gt;(object: T | null | undefined, defaultValue: T | null = null = {} as T) : T {
      if (typeof object === 'undefined' || object === null)
          return defaultValue as T;
      else
          return object;
  }
  </code></pre>
  
  <p>App.tsx (root TypeScript file):</p>
  
  <pre><code>import 'Global';
  </code></pre>
  
  <p>Other TSX file (method usage):</p>
  
  <pre><code>s(s(nullableVar).member).member; //Runtime error
  </code></pre>
  
  <p>This compiles fine, however, in the browser this throws '<code>s is not a function</code>'.</p>