Should JavaScript Add a New Syntax for Types?

An interesting tidbit from Mike Melanson’s column, "This Week in Programming."

“If a proposal unveiled this week gets its way, JavaScript developers will soon have something that many of them have long been asking for: a type system, of some sort at least.”

A blog post by TypeScript senior program manager Daniel Rosenwasser lays out the background and reasoning for the proposal for type syntax in JavaScript. He writes that “if we pull this all off, we have the chance to make one of the most impactful improvements to the world of JavaScript.”

The proposal, which shares authors from Microsoft, Bloomberg, Igalia, and a number of other sources, suggests that JavaScript developers should be able to “add type annotations to their JavaScript code, allowing those annotations to be checked by a type checker that is external to JavaScript” and then be ignored at runtime. “Because this new syntax wouldn’t change how surrounding code runs, it would effectively act as comments,” writes Rosenwasser in his blog post, later adding that “JavaScript could carve out a set of syntax for types that engines would entirely ignore, but which tools like TypeScript, Flow, and others could use…”

One noteworthy part of the proposal lays out exactly what is not being proposed:

“Our team isn’t proposing putting TypeScript’s type-checking in every browser and JavaScript runtime — nor are we proposing any new type-checker to be put in the browser. We think doing that would cause problems for JavaScript and TypeScript users alike due to a range of issues, such as runtime performance, compatibility issues with existing TypeScript code, and the risk of halting innovation in the type-checking space.” Similarly, several features from TypeScript that generate code, such as enums, namespaces, and parameter properties, are being explicitly excluded “because they have runtime semantics, generating JavaScript code rather than simply being stripped out and ignored…”

As the proposal’s authors note, the proposal itself is presented as a “strawperson proposal”… Thus far, it would appear that there is debate aplenty, alongside a rather robust enthusiasm for the advent of type functionality coming to a JavaScript near you.

1 Like