Today we’re excited to announce our support and collaboration on a new Stage 0 proposal to bring optional and erasable type syntax to JavaScript. Because this new syntax wouldn’t change how surrounding code runs, it would effectively act as comments. We think this has the potential to make TypeScript easier and faster to use for development at every scale. We’d like to talk about why we’re pursuing this, and how this proposal works at a high level.
Background
One recent trend our team has seen in the JavaScript world is a demand for faster iteration time and a reduction of build steps. In other words, "make it faster and make it simpler".
In some ways, this is already happening. Thanks to the success of evergreen browsers, developers can often avoid compiling newer versions of JavaScript to run on older runtimes. To some extent the same is also true of bundling – most browsers have built-in support for using modules, so bundling can be viewed as more of an optimization step than a necessity. This has increasingly been the case, so how is TypeScript keeping up?
If we go back to 2012 when TypeScript was first announced, the JavaScript world was drastically different! Some browsers shipped often, but not all. It was unclear how long we’d be stuck with ancient versions of Internet Explorer, and that led to tools like bundlers and compilers gaining adoption. TypeScript was able to really thrive in the age where adding a build step to JavaScript was a given – after all, if you need to compile your JavaScript anyway, why not compile away your types too? But if those trends we mentioned above continue, compiling away your types might be the only step between writing your TypeScript and running it, and we don’t want to be the ones standing in the way of a good developer experience!
In some ways, our JavaScript support bridges the gap here, and maybe you’ve seen this if you use an editor like Visual Studio or Visual Studio Code.
Today, you can create a .js
file in your editor and start sprinkling in types in the form of JSDoc comments.
/**
* @param a {number}
* @param b {number}
*/
function add(a, b) {
return a + b;
}
Because these are just comments, they don’t change how your code runs at all – they’re just a form of documentation, but TypeScript uses them to give you a better JavaScript editing experience through things like code completions, refactorings, and more.
You can even add type-checking by adding a // @ts-check
comment to the top of your file, or running those files through the TypeScript compiler with checkJs
.
This feature makes it incredibly convenient to get some of the TypeScript experience without a build step, and you can use it for small scripts, basic web pages, server code in Node.js, etc.
Still, you’ll notice that this is a little verbose – we love how lightweight the inner-loop is for writing JavaScript, but we’re missing how convenient TypeScript makes it to just write types.
So what if we had both?
What if we could have something like TypeScript syntax which was totally ignored – sort of like comments – in JavaScript.
function add(a: number, b: number) {
return a + b;
}
Our team believes there is a lot of potential here, and this month we’re hoping to bring it forward in a proposal to TC39, the ECMAScript standards committee!
How Would This Work?
When we’ve been asked "when are types coming to JavaScript?", we’ve had to hesitate to answer. Historically, the problem was that if you asked developers what they had in mind for types in JavaScript, you’d get many different answers. Some felt that types should be totally ignored, while others felt like they should have some meaning – possibly that they should enforce some sort of runtime validation, or that they should be introspectable, or that they should act as hints to the engine for optimization, and more! But in the last few years we’ve seen people converge more towards a design that works well with the direction TypeScript has moved towards – that types are totally ignored and erasable syntax at runtime. This convergence, alongside the broad use of TypeScript, made us feel more confident when several JavaScript and TypeScript developers outside of our core team approached us once more about a proposal called "types as comments".
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.
So when it comes to writing and running code, a developer’s inner-loop would look a little different.
Meanwhile, writing code and type-checking would stay the same. A developer could get instant type-checking feedback in an editor with TypeScript support, run TypeScript on the command line, and add TypeScript as part of their CI tasks. The biggest difference is that because we would not need a build step, we would dramatically lower the barrier to entry for JavaScript devs to experience the power of types and great tooling.
To make this happen, JavaScript would minimally need to add syntax for things like type annotations on variables and functions, optionality modifiers (?
) for parameters and class members, type declarations (interface
s and type
aliases), and type assertion operators (as
and !
) – all of which would have no effect on how the surrounding code is run.
Things like visibility modifiers (e.g. public
, private
, and protected
) might be in scope as well; however, enums, namespaces, and parameter properties would be out of scope for this proposal since they have observable runtime behavior.
Those features could be proposed as separate ECMAScript features based on feedback, but our current goal is to support some large subset of TypeScript that we think could be a valuable addition to JavaScript.
With this carve out, we’ve left room for type-checkers to innovate in ways that require new syntax. That does mean that engines would happily run code with nonsensical types, but we believe type-checkers could (and should) be prescriptive and enforce stricter constraints than runtimes. Combined, this makes for a type syntax that could be customized across different checkers, or removable entirely if someone decides they’re not happy with TypeScript or any other type-checker.
What is this not?
It’s worth mentioning what this proposal isn’t.
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.
Instead, we’re just proposing syntax that is compatible with and motivated by TypeScript, which could be used by any type-checker, but which would skipped over by JavaScript engines. We believe that this approach is the most promising for everyone, and would continue to allow TypeScript, Flow, and others to continue to innovate.
What’s next?
Given all this, we plan to present this proposal for Stage 1 at the upcoming March 2022 plenary meeting of TC39. We’ll be doing so with the support and guidance from our co-champions of this proposal, Rob Palmer at Bloomberg and Romulo Cintra at Igalia.
Reaching Stage 1 would mean that the standards committee believes that supporting type syntax is worth considering for ECMAScript. This isn’t a sure-fire thing – there are many valuable perspectives within the committee, and we do expect some amount of skepticism. A proposal like this will receive a lot of feedback and appropriate scrutiny. It may involve lots design changes along the way, and may take years to yield results.
But if we pull this all off, we have the chance to make one of the most impactful improvements to the world of JavaScript. We’re excited by that, and we hope you are too.
If you’re interested in hearing more about the specifics and current direction, head on over to the proposal repository. We look forward to hearing what you think!
And lastly, the TypeScript team and the champions group would like to recognize and extend our thanks to all those who worked on prior art, along with the contributors who reached out to help with types as comments, and especially Gil Tayar who helped spearhead it. We’re grateful to be part of such a passionate community!
Can’t wait to have this approved and ready to go! I just want to stop compiling my libraries.
I was looking for the proposal of typing in Javascript for knowing how far we are in the process. The proposal was submitted just last month? I am not able to propose such a thing by myself, but I thought it was something from years ago!
I don't like TypeScript, but I think it has been a big reference for the community by now and proposing two ways of typing is too expensive for parsers. Yes,...
Awesome idea. I hate TS for all the usual reasons, but this proposal is the compromise a 15-year vanilla JS advocate can be satisfied with.
I’d love to embrace the idea, just convince me you are not trying to EEE with this.
fantastic!!!!
I am against this because JSDoc is already there and perfect. What I see is you just want to make a syntax that is shorthand for JSDoc. It is JSDoc in disguise. And it make a change that not compatible while JSDoc is completely compatible
I would suggest please just make a better JSDoc and all would be good
Leave comment be a comment
On one hand, I can see this handy during development. Maybe you are running code in the browser as you build (pretty typical) which would mean one less compiling step each time you save. It would also make copy/pasting TS code into the browser environment supported.
Am I going to stop compiling, transpiling, tree-shaking, etc... ? No way. No one who needs to build efficient web apps will either. Some might argue that the compiling/transpiling...
ES2024, run native JS: ‘expression produces a type that is too complex to represent’. A dream!
I don't understand why this is very exciting to anyone. All this does is further complicate the JavaScript language in the service of two popular type checking tools. We will still need those tools for any type safety guarantees. And if we need those tools anyway, how does adding this complex comment syntax that also needs to support special keywords like `type` and `interface`, as well as generics to JavaScript really help anyone?
If the goal...