javascriptinterfacetype-safety

Typesafe Javascript


Is there a way to enforce types in JavaScript? I'm thinking of a pre-processor which takes an input file written in ActionScript 3 or Java and converts it to JS.

I do not need a big run-time apparatus, I just need to introduce the idea of compile-time in my workflow and run the trivial compile-time checks on my code (and also use interfaces). Neither I need the API from Java or Flex, just the syntax.

The standard browser-functions could also be checked against the IDL definitions, but it is not a must.


Solution

  • You should take a look at the haxe project.

    Haxe is a very nice typed language that uses type inference (i.e. you're not forced to write a lot of type declarations) but that enforces type correctness at compile time.

    The language has a javascript-like syntax and the compiler can generate code for the neko virtual machine, for javascript, as3, c++ or PHP.

    Update

    Today the most popular choice is probably Typescript, a superset of Javascript that allows optional type declarations that are enforced compile time.