title: How does lazy module loading work in typescript?
body: <p>In this <a href="https://basarat.gitbooks.io/typescript/content/docs/globals.html" rel="nofollow noreferrer">typescript book section</a> the author explains lazy loading with the following example:</p>
  
  <pre><code>import foo = require('foo');
  
  export function loadFoo() {
      // This is lazy loading `foo` and using the original module *only* as a type annotation
      var _foo: typeof foo = require('foo');
      // Now use `_foo` as a variable instead of `foo`.
  }
  </code></pre>
  
  <p>According to the author typescript only loads the type of <code>foo</code> on the first call to <code>require</code> but on the second call when a <code>foo</code> var is created it loads the entire module required to create the <code>var _foo</code>.</p>
  
  <p>How does this work.  Can someone show a more detailed example of what's going on under the hood?</p>