A Proposal For Type Syntax in JavaScript

A very interesting Stage-0 proposal is to bring optional and erasable type syntax to JavaScript.

💁‍♂️ Stage-0?

The Technical Committee which is concerned with the standardization of ECMAScript (i.e. TC39) has a 5 stage process in place, ranging from stage-0 to stage-4, by which it develops a new language feature.

Stage-0 is the Strawperson stage which collects all proposals without any acceptance criteria. It's intended to spark the discussion. It is only when a proposal reaches Stage 4 that it is ready to become part of the ECMAScript Specification.

The proposed syntax looks like this:

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

The type hints wouldn’t be used by JS itself though — so you won’t get a runtime error if you pass something other than a number into that function — but, instead, they’re meant for your IDE (or a tool) to read so that your IDE can give you hints about the argument types while you’re crafting your code.

The idea of this proposal 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. This allows us to keep the things you love about TypeScript – its type-checking and editing experience – while removing the need for a build step in development.

You can compare it to typing some JSDoc above a function: they’re only useful for the IDE, not the JS runtime, and can keep you from writing bad code.

A Proposal For Type Syntax in JavaScript →
ECMAScript proposal: Types as Comments →

Published by Bramus!

Bramus is a frontend web developer from Belgium, working as a Chrome Developer Relations Engineer at Google. From the moment he discovered view-source at the age of 14 (way back in 1997), he fell in love with the web and has been tinkering with it ever since (more …)

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.