Microsoft is championing a new proposal that aims to introduce optional and erasable type annotations in JavaScript, potentially streamlining the development process and making static typing more accessible within the JavaScript ecosystem. This move could have significant implications for TypeScript, Microsoft’s superset of JavaScript, by making it faster and more intuitive to use. The proposal seeks to add type annotations to JavaScript code that would be validated by external type checkers, such as TypeScript or Flow, but ignored by JavaScript engines at runtime. Essentially, the type information would behave like comments, providing the benefits of type checking during development without affecting the execution of the code.
The primary goal of this proposal is to allow developers to use static typing tools like TypeScript and Flow within JavaScript without needing to transpile their code. If developers stick to a broad subset of the language, they could write JavaScript code with type annotations that are purely used for development purposes, improving the type safety of their applications without introducing extra build steps. The new syntax would not alter the way the JavaScript engine processes the code but would allow tools to read and validate type information, enhancing the development experience without sacrificing runtime performance.
This proposal comes at a time when static typing has gained significant traction in the JavaScript world. Languages like TypeScript, Facebook’s Flow, and Google’s Closure Compiler have demonstrated how type annotations can boost productivity and reduce errors in complex applications. Microsoft, along with other contributors like Gil Tayar, Romulo Cintra, and Rob Palmer, pointed out that the case for static typing has been proven successful over the past decade. The proposal’s authors argue that the syntax for adding type information has matured, and most of the time, these types are erased before running the code. By integrating type checking directly into the language without impacting performance, JavaScript could gain the best of both worlds.
Historically, the rise of type syntax in JavaScript coincided with the need for downlevel compilation, or transpiling, to remove type annotations before the code could run. While this was effective, it required an additional build step, which slowed down development cycles. The proposed changes would simplify this process by reducing the need for a separate compilation phase, allowing developers to write JavaScript with type annotations and run it directly, with the type information being erased only when needed. In the long term, this could streamline workflows, making TypeScript and other static typing tools more seamless to use and ultimately improving the efficiency of JavaScript development.