Menu
Microsoft pushes optional type annotations for JavaScript

Microsoft pushes optional type annotations for JavaScript

Standards proposal would allow annotations to be checked by external type checkers like TypeScript but treated as comments by the JavaScript engine.

Credit: Dreamstime

Microsoft is backing a proposal to bring optional and erasable type syntax to JavaScript, potentially making the company’s TypeScript language, which builds on JavaScript, faster and easier to use.

The standards proposal calls for allowing type annotations to be added to JavaScript code that would be checked by type checkers external to JavaScript. At runtime, a JavaScript engine would treat the types as comments. 

The plan calls for JavaScript to carve out of a set of syntax for types that engines would ignore but tools such as TypeScript, Flow, and others could use. This proposal would add optional annotations to ECMAScript, the official standard underlying JavaScript.

The plan’s intent is to allow developers to run programs in TypeScript, Flow, and other static typing supersets of JavaScript with no need for transpilation, if developers remain within a certain reasonably large subset of the language. The new syntax would not change how surrounding code runs and would effectively act as comments, Microsoft said in a bulletin on March 9.

In explaining the motivation, the authors of the proposal including Gil Tayar, Daniel Rosenwasser of Microsoft, Romulo Cintra of Igalia, and Rob Palmer of Bloomberg wrote that, during the past decade, the case for static type checking has been proven out fairly successfully. 

Large investments in JavaScript such as Microsoft’s TypeScript, Google’s Closure Compiler, and Facebook’s Flow have provided convenient syntax for declaring and using types in JavaScript and have reaped productivity gains seen in other statically typed languages. 

Their syntax mostly does not impact runtime semantics; in practice, most of the work in converting these variants to plain JavaScript amounts to erasing types. Furthermore, static typing has been a heavily requested feature by JavaScript programmers.

The rise of type syntax in JavaScript coincided with the rise of downlevel compilation, or transpilation. Because type syntax is not natively supported in JavaScript, a tool had to exist to remove types before running code. For systems like TypeScript, it made sense to integrate a type erasure step with syntax downleveling. 

Some bundlers have done both. Over time, it is anticipated there will be less need to downlevel compile. Eventually, the only step for many TypeScript users between writing and running code will be to erase type annotations. The proposal will reduce the need to have a build step. Users will simply run the code they wrote.

Microsoft is not calling for putting TypeScript’s type checking in every browser and JavaScript runtime, or for putting a new type checker in the browser. The company believes this would cause runtime performance and compatibility problems for JavaScript and TypeScript users and also risk stopping innovation. 

Instead, Microsoft is proposing a JavaScript syntax compatible with TypeScript that could be used by any type checker but ignored by JavaScript engines.


Tags Microsoftjavascript

Brand Post

Show Comments