title: TypeScript: problems with type system
body: <p>I'm just testing typescript in VisualStudio 2012 and have a problem with its type system. My html site has a canvas tag with the id "mycanvas". I'm trying to draw a rectangle on this canvas. Here's the code</p>
  
  <pre><code>var canvas = document.getElementById("mycanvas");
  var ctx: CanvasRenderingContext2D = canvas.getContext("2d");
  ctx.fillStyle = "#00FF00";
  ctx.fillRect(0, 0, 100, 100);
  </code></pre>
  
  <p>Unfortunately VisualStudio complains that </p>
  
  <blockquote>
    <p>the property 'getContext' does no exist on value of type
    'HTMLElement'</p>
  </blockquote>
  
  <p>It marks the second line as an error. I thought this would be merely a warning but the code does not compile. VisualStudio says that </p>
  
  <blockquote>
    <p>there were build errors. Would you like to continue and run the last
    successful build ?</p>
  </blockquote>
  
  <p>I didn't like this error at all. Why is there no dynamic method invocation ? After all the method getContext definitely exists on my canvas element. However I thought this problem would be easy to solve. I just added a type annotiation for canvas:</p>
  
  <pre><code>var canvas : HTMLCanvasElement = document.getElementById("mycanvas");
  var ctx: CanvasRenderingContext2D = canvas.getContext("2d");
  ctx.fillStyle = "#00FF00";
  ctx.fillRect(0, 0, 100, 100);
  </code></pre>
  
  <p>But the type system still wasn't satisfied. Here's the new error message, this time in the first line:</p>
  
  <blockquote>
    <p>Cannot convert 'HTMLElement' to 'HTMLCanvasElement': Type
    'HTMLElement' is missing property 'toDataURL' from type
    'HTMLCanvasElement'</p>
  </blockquote>
  
  <p>Well, I'm all out for static typing but this makes the language unusable. What does the type system want me to do ?</p>
  
  <p>UPDATE:</p>
  
  <p>Typescript has indeed no support for dynamic invocation and my problem can be solved with typecasts. My question is basically a duplicate of this one <a href="https://stackoverflow.com/questions/12686927/typescript-casting-htmlelement?rq=1">TypeScript: casting HTMLElement</a></p>