title: How to write a Typescript class definition for a module
body: <p>I want to write a Typescript definition file (.d.ts) for this module <a href="https://www.npmjs.com/package/canvas" rel="nofollow">https://www.npmjs.com/package/canvas</a><br></p>
  
  <p>So here's what I've started with:</p>
  
  <pre><code>declare module Canvas {
      class Canvas {
          constructor(width: number, height: number);
      }
  }
  
  declare module "canvas" {
      export = Canvas;
  } 
  </code></pre>
  
  <p>This is working when I import &amp; call the module like this:</p>
  
  <pre><code>/// &lt;reference path="./canvas.d.ts" /&gt;
  import * as Canvas from "canvas";
  new Canvas.Canvas(32, 32)
  </code></pre>
  
  <p>However, I want to call the class like this:</p>
  
  <pre><code>new Canvas(32, 32)
  </code></pre>
  
  <p>I also want to add two things to the class:</p>
  
  <ul>
  <li>It should return an <code>HTMLCanvasElement</code> so It can work just like the normal  Canvas api</li>
  <li>I also want to add an .Image property to the module which is accessible without constructing the class. </li>
  </ul>
  
  <p>Here's an example of what I'm waiting for:</p>
  
  <pre><code>import * as Canvas from "canvas";
  var Image  = Canvas.Image;
  var canvas = new Canvas(32, 32);
  var ctx    = canvas.getContext('2d');
  
  ctx.drawImage and every regular functions...
  [...]
  </code></pre>
  
  <p>I also want to add an optional callback parameter to <code>canvas.toDataURL()</code>.</p>
  
  <p>How can I do that? </p>