title: Typescript call function that may not exist at compile time
body: <p>I am trying to write a library that checks for a callback function and then calls it when it is ready. The idea being that you can include the JS asynchronously and then when it's loaded it will check for the callback and execute it.</p>
  
  <p>I'm using typescript. The following JS works but wont compile in TS</p>
  
  <pre><code>if(typeof myFramework.ready === 'function') {
      myFramework.ready();
  }
  </code></pre>
  
  <p>Then the developer using the library could do the following:</p>
  
  <pre><code>var myFramework = myFramework || {}; // As the framework wont exists
  myFramework.ready = function(){//Execute developer's code};
  </code></pre>
  
  <p>However, using TypeScript I'm getting a type error: Property 'ready' does not exist on type 'typeof myFramework' - which is not a surprise. It does compile if I do the following:</p>
  
  <pre><code>interface Window {
      ready(): void;
  }
  
  if(typeof window.ready === 'function') {
      window.ready();
  }
  </code></pre>
  
  <p>But that would mean adding more crud to the window object. And I imagine calling a global ready() method could well be a point of failure!</p>
  
  <p>How would you go about achieving this?</p>
  
  <hr>
  
  <p>Thanks to @james-crosswell - as I have classes in separate files the file that had the Ajax handling bit in looked something like:</p>
  
  <pre><code>module MyFramework {
      export interface IMyFramework {
          ready? : () =&gt; any;
      }
  
      export class HasLoaded implements IMyFramework {
          doReady = (framework: IMyFramework) =&gt; {
              if(typeof framework.ready === 'function') {
                  framework.ready();
              }
          }
      }
  
      var newHost: HasLoaded = new HasLoaded();
      newHost.doReady(MyFramework);
  }
  </code></pre>
  
  <p>I may be rethinking the logic that got me here but that's a separate issue!</p>