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.



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.


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


Whilst I'm a little late to this party, I think it's definitely worth mentioning Dart (which is a Google product) and TypeScript (which is a Microsoft product).

JavaScript is fast becoming an extremely popular language as applications become more web based. However as you have pointed out, JavaScript lacks type safety, and to name a few other things; classes, interfaces and packages/namespaces/modules.

This is where Dart and TypeScript step in. These languages are essentially supersets of JavaScript. When you write Dart or TypeScript code, it is compiled into clean, standards compliant JavaScript.

The benefits of Dart and TypeScript are that they provide type safety, interfaces, classes etc. Thus allowing you to write cleaner, scalable, manageable applications, which still run in the browser.

Being a Microsoft oriented programmer, I've had a lot of experience with TypeScript, including being somewhat active in the development of the language (you can find information for TypeScript development at codeplex)

My only concern at the moment is that TypeScript is lacking in some fundamental features. It seems that some of the current implementation (0.9.0 alpha) has some equally gaping holes that might deter the savvy developer from using it at the moment (subject to change of course).

I cannot really comment on Dart, as I have only used this a few times, but my overall experience with Dart was good!


GWT does what looking for, but its a way oversized for the most cases. You could take a look at googles closure framework which fakes the typed safe with anotations


There are many statically-typed languages that are designed with the specific goal of compiling down to JavaScript (the so-called assembly of the web):


While typeof will return 'object' for every object or array, you can use the instanceof statement. Say you have a class Person, and want to see whether the object passed to your function is a Person, you can do this:

function someFunc(person){
  if(! person instanceof Person)
    throw('argument needs to be an instance of Person.');

  /* ... do your stuff ... */

If you just want to make sure a variable is the number 3 instead of a string '3', you only need to use === instead of ==:

if( var === 3 ){
  /* ... do your stuff ... */

I agreed that Javascript is a beautiful language, with some glaring holes, the worst and most unremarked of which is the absence of static type-safety.

As eskimoblood pointed out, there are some half-measures in the form of GWT and Closure but the right answer, imo, is Scala, which combines Javascript's flexibility and expressive power with a type system much better than Java's -- or that would be the right answer except that the Scala-GWT project seems to have gotten bogged down.

For now, we wait...


Infernu is a type-safe subset of JavaScript. It doesn't compile into JavaScript - it can run as-is in your browser! The type checker supports full type inference, so no type annotations are needed (in the future they will be supported for documentation and for deliberate distinction between structurally identical types). It isn't ready yet for usage, but a work in progress.


Here's a lightweight typesafe function wrapper for nodejs/browser:


Its 3.9K gzipped, supports jsonschema, and works pretty much everywhere. So:

foo = function(bar){


could be rewritten like so:

foo = typesafe({ 
  bar: { type: "object" } 
}, function(bar){


Or just wrapped later on, including validating deepnested structures, and optional args:

foo = typesafe({
  bar: { 
    type: "object", 
    required: true,
    properties: {
      foo: { type: "string", required:true },
      items: [{
        type: "integer"  
}, foo ); 

foo({ foo: "helloworld", items:[1,2,3] });

It looks less noisy in coffeescript imho


Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.