This article is more than 1 year old

Microsoft proposes type syntax for JavaScript

Long overdue innovation or an affront to all that developers hold dear?

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

The proposal planned from Microsoft and others, including devs from the Igalia Coding Experience program and Bloomberg, is all about adding "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 (such as Microsoft's own TypeScript) and ignored by those that do not.

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

JavaScript survey: Most use React but satisfaction low

READ MORE

The popularity of tools such as TypeScript and Flow, with built-in type checking, has demonstrated that a demand exists. The most recent State of JavaScript survey highlighted Static Typing as something many were thirsting for.

The thing is, there is wide variation in opinions about what Static Typing actually represents in the world of JavaScript, not to mention a healthy subset that would run a mile from such functionality. The proposal from the group of devs led by Microsoft is a compromise position. At present, in the TypeScript world, one can express types in JSDoc comments, which tend to be a little verbose and, dare we say it, clunky in use.

The example given by Microsoft's Daniel Rosenwasser of current TypeScript JSDoc comment practise is:

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

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

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

"The idea of this proposal," said Rosenwasser, "is 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."

Rosenwasser was quick to emphasize that the proposal is not about sticking TypeScript type-checking into every JavaScript runtime (certainly, one could imagine all kinds of compatibility problems 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 accustomed to using a type checker, then the approach has much to commend it. However, by carefully making sure everything remains optional there is also the risk it might just complicate things further and simply be ignored.

Unsurprisingly, debate over the move has rumbled on since its publication last week. Some think the idea is an excellent one while others are a little more negative.

Rosenwasser said: "A proposal like this will receive a lot of feedback and appropriate scrutiny."

It is also still only a proposal at the moment, planned for Stage 1 of the ECMA TC39 process. There are therefore many hurdles ahead before the concept (if accepted) makes its way into the form ECMAScript and plenty of opportunity for debate.

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

More about

TIP US OFF

Send us news


Other stories you might like