The ECMAScript proposal “Types as comments” (by Gil Tayar, Daniel Rosenwasser, Romulo Cintra, Rob Palmer, and others) is about adding type annotations to JavaScript (there is also an accompanying blog post).
Such type annotations would look similar to TypeScript’s and Flow’s annotations and are completely ignored at runtime.
In this blog post, I briefly explain how the proposed type annotations would work and then describe what I think about them.
This is an example:
function add(x: number, y: number) {
return x + y;
}
The parameters x
and y
have type annotations: The type number
, separated by a colon.
This is how these annotations are handled:
At runtime, JavaScript engines completely ignore them – as if they were comments.
At development time, type checkers can statically analyze the annotations and warn developers about potential issues.
So far, we have only seen relatively simple type annotations. More complicated ones are also proposed – e.g.:
interface
)type
)as
)add<number>(4, 5)
)Upsides:
Having a standard for type notations would be good and would make it easier for tooling and experiments in this field.
It would become possible to program (e.g.) TypeScript without compiling the source code. There would only be type checking at development time. This would considerably improve the development experience for statically typed JavaScript:
.d.ts
files often won‘t be needed either.This development experience is similar to providing type information via JSDoc comments – which is already a popular way of using TypeScript.
Downsides:
Various thoughts:
I can understand if JavaScript developers are afraid of TypeScript taking over their language. However, this proposal will be as far as things will go w.r.t. adding TypeScript features to JavaScript. From the viewpoint of JavaScript engines, type annotations will be more like comments.
What are your thoughts? Let us know in the comments!
@SeaRyanC
[Source: tweet thread]
Here’s one way to think about this.
Would you ever intentionally ship unminified JS to your production website? Probably not
Do you write minified code today? Definitely not.
But is it useful to be able to run unminified code? Extremely!
Removing type annotations is the same thing. And just like your lint rules might enforce a style guide, your choice of type checker can validate type annotations you write.
Runtimes ignore both style and type “errors”, both of which vanish completely in minfication.
You could imagine a world where JS didn’t allow indentation. We’d probably write a compile-to-JS language (or many) that allowed indentation, maybe with style rules.
“Indentation in the browser” would be the idea of just allowing indentation, not re-implementing those style rules.
@orta
[Source: tweet thread]
One angle I don’t think people have explored when thinking about ‘Types As Comments’ is how much modern JS development has moved to a ‘soft fork’ of the JS language for types/tools.
‘Types as Comments’ would allow a lot of TS codebases to move back towards alignment with JS.
The TS design goals are set up to ensure that TypeScript stays as an non-hostile ‘soft fork’ of the language, thus ‘JS + types’ as the one-liner. Now with ‘Types as Comments’ allowing for ‘types in JS’ to not be a fork but a legit subset instead of superset.
@_nicojs
: “Reading through [the proposal], it is clear that this is not going to be easy. It’s mainly hard to decide where type comments start and where they stop.
For example: function foo(bar: baz) {}
seems simple, but what about function foo<bar>(baz: { qux: corge }) {}
?
[...]
Did you know that add<number>(4, 5)
is valid JS? It makes sense when you think about it, just never did.”
@alexandereardon
: “If I worked on a compiler and I was tasked with making things faster, I would be reaching for those annotations to help pre-optimise code as much as I could!”
@AndaristRake
: “The only benefit from my PoV is that I would be able to copy my TS code and evaluate it quickly in a console etc, without stripping the types first.”
A quick introduction to TypeScript’s type notation: chapter “The essentials of TypeScript” in “Tackling TypeScript”
Using TypeScript via JSDoc comments and pure JavaScript: “JSDoc reference” in the TypeScript handbook
Blog post: “A proposal for type syntax in JavaScript” by Daniel Rosenwasser
ECMAScript poposal: “Types as comments” by Gil Tayar, Daniel Rosenwasser, Romulo Cintra, Rob Palmer, and others