Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeScript: problems with type system

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

var canvas = document.getElementById("mycanvas"); var ctx: CanvasRenderingContext2D = canvas.getContext("2d"); ctx.fillStyle = "#00FF00"; ctx.fillRect(0, 0, 100, 100); 

Unfortunately VisualStudio complains that

the property 'getContext' does no exist on value of type 'HTMLElement'

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

there were build errors. Would you like to continue and run the last successful build ?

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:

var canvas : HTMLCanvasElement = document.getElementById("mycanvas"); var ctx: CanvasRenderingContext2D = canvas.getContext("2d"); ctx.fillStyle = "#00FF00"; ctx.fillRect(0, 0, 100, 100); 

But the type system still wasn't satisfied. Here's the new error message, this time in the first line:

Cannot convert 'HTMLElement' to 'HTMLCanvasElement': Type 'HTMLElement' is missing property 'toDataURL' from type 'HTMLCanvasElement'

Well, I'm all out for static typing but this makes the language unusable. What does the type system want me to do ?

UPDATE:

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 TypeScript: casting HTMLElement

like image 545
lhk Avatar asked Dec 02 '12 12:12

lhk


People also ask

Is it bad to use any type in TypeScript?

❌ Don't use any as a type unless you are in the process of migrating a JavaScript project to TypeScript. The compiler effectively treats any as “please turn off type checking for this thing”.

What are the problems with TypeScript?

TypeScript will only check types at compile time and only types that are available. Any network calls, system libraries, platform-specific APIs and non-typed third-party libraries have no way of communicating with TypeScript.

What type system does TypeScript use?

The type system in TypeScript is designed to be optional so that your JavaScript is TypeScript. TypeScript does not block JavaScript emit in the presence of Type Errors, allowing you to progressively update your JS to TS.

Is TypeScript typing strong?

TypeScript is JavaScript with syntax for types. TypeScript is a strongly typed programming language that builds on JavaScript, giving you better tooling at any scale.


2 Answers

var canvas = <HTMLCanvasElement> document.getElementById("mycanvas"); var ctx = canvas.getContext("2d"); 

or using dynamic lookup with the any type (no typechecking):

var canvas : any = document.getElementById("mycanvas"); var ctx = canvas.getContext("2d"); 

You can look at the different types in lib.d.ts.

like image 116
Markus Jarderot Avatar answered Oct 11 '22 17:10

Markus Jarderot


const canvas =  document.getElementById('stage') as HTMLCanvasElement; 
like image 43
Halil Kocaerkek Avatar answered Oct 11 '22 17:10

Halil Kocaerkek