Question

Is there any support for static typing in ECMAScript 6? How about ECMAScript 7?

Was it helpful?

Solution

No.

But on the ECMA-Script Wikipage there is a paragraph about changes in ECMA-Script 7:

The Seventh Edition is in a very early stage of development, but is intended to continue the themes of language reform, code isolation, control of effects and library/tool enabling from ES6. New features proposed include promises/concurrency, number and math enhancements, guards and trademarks (an alternative to static typing), operator overloading, value types (first-class number-like objects), new record structures (records, tuples and typed arrays), pattern matching, and traits.

Which may interest you.

OTHER TIPS

Though this isn't part of the ES6 spec, Closure Compiler enforces JSDoc argument type annotations in JavaScript code when using its Advanced compilation level. Type annotations are specified using comments so they're ignored in development, but when you build your app for a production release a type mismatch will result in a compiler warning or, optionally, a compiler error.

An example of an enforced JSDoc type annotation:

/**
 * @param {string} stringValue
 * @return {number}
 */
function toInt(stringValue) {
  return parseInt(stringValue, 10);
}

var val = toInt("10"); // Good
var val = toInt(false); // NaN in development, but throws an error (optional) 
                        // or prints a warning (default) at build time

As an added bonus, JSDoc can build API documentation using this same syntax. So it's also handy if you document your code.

But a warning: for Closure Compiler to do its advanced optimization magic, every engineer on your project has to follow certain strict coding conventions. The compiler cannot enforce types unless it can reliably figure out what your code is doing, and that means giving up some of JavaScript's dynamic and wishy-washy syntax. If you don't follow them, errors can creep into your app and they can be very hard to diagnose after the fact. Most popular JavaScript frameworks and libraries do not follow them, though you can sometimes work around that using Compiler's externs feature. (jQuery is supported using externs, for example.)

So if you do use it, make sure you test your app thoroughly. I personally wouldn't even consider using this feature on a web app unless it has a Jenkins build bot and near-100% automated test coverage that can be run against your code after it's been optimized. That's a lot of work and is not for everyone; it took me months to get one of my projects up to that level. But personally, I think it's well worth the effort.

For more information, check out Advanced Compilation and Externs and Annotating JavaScript for the Closure Compiler.

No, there is no support for static typing in either ECMAScript 6 (ES2015).

As for ECMAScript 7 (ES2016), there is no proposal at any of stages 1, 2, 3, 4 or stage 0 for static typing.

I've seen a few proposals/ideas for static typing appear on the es-discuss mailing list, but none of these have actually been proposed for ES7 (ES2016).

If you want static typing right now, you're probably best looking into TypeScript or Flow.

Althought it's not pure ES6, Google's AtScript extending ES6 with type annotations and compiles into valid ES6 code once the compiler gets public: AtScript primer

As an option you can take a look at EsLint plugin https://github.com/yarax/typelint

It's not static check, but optional. The benefit of TypeLint is using already existing app data to build and use types automatically, unlike for example TypeScript or Flow, where you have to describe complex types by yourself.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top