Proposed type syntax for JavaScript • The Register

0

Type fans, rejoice! Plans (or a proposal, at least) are afoot to bring type checking into the infamous dynamically typed JavaScript.

The proposal planned by Microsoft and others, including developers of the Igalia Coding Experience program and Bloomberg, is to add “types as comments” to the language.

The theory is that it would become possible to have a syntax for types that could be used by tools that need those hooks (like Microsoft’s own TypeScript) and ignored by those that don’t.

Types are a controversial and, some would say, long overdue innovation for venerable JavaScript.

JavaScript survey: most use React, but satisfaction is low

READ MORE

The popularity of tools such as TypeScript and Flow, with built-in type checking, has demonstrated that there is demand. The most recent State of JavaScript survey highlighted static typing as something many have been craving.

The thing is, there is a wide diversity of opinions about what Static Typing really stands for in the world of JavaScript, not to mention a healthy subset that would shy away from such a feature. The proposal from the group of developers led by Microsoft is a compromise position. Right now, in the TypeScript world, you can express types in JSDoc comments, which tend to be a bit verbose and, dare we say, clunky to use.

The example given by Microsoft’s Daniel Rosenwasser of the current practice of TypeScript JSDoc comments is:

/**
 * @param a {number}
 * @param b {number}
 */
function add(a, b) {
    return a + b;
}

Which could, if the proposal is accepted, become something like:

function add(a: number, b: number) {
    return a + b;
}

“The idea behind this proposal,” Rosenwasser said, “is that JavaScript could create a set of syntaxes for types that engines would ignore entirely, but tools like TypeScript, Flow, and others could use.”

Rosenwasser was quick to point out that the proposal is not to stick TypeScript type checking into every JavaScript runtime (surely, one could imagine all sorts of compatibility issues down the line) instead, the plan is that the approach would be picked up by any type checker, not just the likes of TypeScript.

Certainly, if a developer is used to using a type checker, then the approach has a lot to offer. However, by carefully ensuring that everything remains optional, there is also the risk that it will complicate things further and simply be ignored.

Unsurprisingly, the debate over this decision has rumbled since it was published last week. Some think the idea is great while others are a bit more negative.

Rosenwasser said, “A proposal like this will get a lot of commentary and proper consideration.”

This is also only a proposal at this time, planned for Stage 1 of the ECMA TC39 process. So there are many hurdles to overcome before the concept (if accepted) finds its way into ECMAScript form and many opportunities for debate.

However, judging by the responses of just over 16,000 developers who answered the State of JavaScript 2021 survey questions, there is an appetite for some sort of type functionality. The developers’ proposal is a first step on the way. ®

Share.

About Author

Comments are closed.