Microsoft pushes optional type annotations for JavaScript

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

The proposed standards call 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 types as comments. The plan calls for JavaScript to carve out a set of syntaxes for types that engines would ignore but tools like TypeScript, Flow, and others could use. This proposal would add optional annotations to ECMAScript, the official standard underlying JavaScript.

The intent of the plan is to allow developers to run programs in TypeScript, Flow, and other statically typed supersets of JavaScript without the need for transpilation, if developers stay within some reasonably large subset of the language. The new syntax would not change how surrounding code executes and would effectively act as comments, Microsoft said in a March 9 bulletin.

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 over the past decade the case for static type checking has been proven quite 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 languages. statically typed. Their syntax generally does not impact runtime semantics; in practice, most of the work of converting these variants to plain JavaScript comes down to erasing types. Additionally, static typing is a much-requested feature by JavaScript programmers.

The rise of type syntax in JavaScript coincided with the rise of lower-level compilation, or transpilation. Since type syntax is not natively supported in JavaScript, a tool had to exist to remove types before executing code. For systems like TypeScript, it made sense to integrate a type erasure step with a syntax upgrade. Some bundlers have done both. Over time, it is expected that there will be less need to compile at a lower level. Eventually, the only step for many TypeScript users between writing and running code will be clearing type annotations. The proposal will reduce the need for a construction stage. Users will simply run the code they have written.

Microsoft is not calling for putting TypeScript’s type checking in every browser and JavaScript runtime, or putting a new type checker in the browser. The company believes this would cause performance and runtime compatibility issues for JavaScript and TypeScript users and also risk stopping innovation. Instead, Microsoft offers a TypeScript-compatible JavaScript syntax that could be used by any type checker but ignored by JavaScript engines.

Copyright © 2022 IDG Communications, Inc.

Comments are closed.