Introducing CommaScript: statically typed JavaScript without the fuss

I just introduced a preview version of a personal project I've been working on for a few months now. It's a variant of JavaScript that I am calling CommaScript, and yes, I'm open to naming suggestions :). The key feature of CommaScript is that it provides type safety in JavaScript without technically requiring anything extra. The project is heavily influenced by some of the philosophy and design decisions behind Mozilla's asm.js project.

There have been several previous attempts at providing static typing in JavaScript, such as TypeScript and Dart. Both languages are great languages, but they suffer from one fatal flaw: they aren't JavaScript!

Mozilla's asm.js project is another attempt at statically typed JavaScript, which is a proper subset of JavaScript meaning it will runs in all JavaScript engines, even the ones that don't support asm.js. I think that Mozilla is really on to something with asm.js because it fits with the spirit of the web, unlike Dart or TypeScript in my opinion. The primary flaw with asm.js is that it looks a lot like assembly, and is almost as difficult to program in.

CommaScript aims to have the backwards compatibility and general philosophy of asm.js combined with the expressiveness of TypeScript and Dart. Specifically, CommaScript has the following goals:

  • 100% JavaScript compliant
  • Compiled and uncompiled CommaScript code is semantically identical
    • As a result, uncompiled CommaScript code runs the same as compiled code
  • CommaScript code should still feel like JavaScript as much as possible

So far I have spec'ced out maybe half of the language and have implemented a rudimentary compiler with unit tests for this part of the spec. You can check out more details on the GitHub project page. To give you a taste of what CommaScript code looks like, check out the following syntactically correct sample:

'use commascript';

var b = true,  
    n = 10,
    s = 'Hello World';

('define(object,foo)', {
    properties: {
        'bar': 'string',
        'baz': 'number'
    }
});

var obj = ('cast(foo)', null);  
obj = {  
    'bar': 'hello world',
    'baz': 10
};
obj.bar = 'goodbye';  
obj.baz = 20;  

Then compare it with this code that contains CommaScript syntax errors

'use commascript';

var b = true,  
    n = 10,
    s = 'Hello World';

// Throws a syntax error because '10' is not a boolean
b = 10;

('define(object,foo)', {
    properties: {
        'bar': 'string',
        'baz': 'number'
    }
});

var obj = ('cast(foo)', null);  
obj = {  
    'bar': 'hello world',
    // Throws a syntax error because 'boo' is not a number
    'baz': 'boo' 
};
// Throws a syntax error because '10' is not a string
obj.bar = 10;  

I first got the idea for CommaScript on the flight home from JSConf at the end of May and have been refining the idea ever since. I feel like it's finally in a  good enough state to show off a little. It is released under the MIT license and is currently distributed as an NPM module. It's in a very early state, pre-alpha even, but I have a ton of ideas for this project and I'm extremely excited about it's possibilities. I can't wait to see where it goes!