The esteemed Dr. Axel Rauschmayer has written a blog post about a new proposal for JavaScript: Types as Comments. It definitely has got me thinking.
On the one hand, I'm a huge fan of JavaScript evolution over the last decade or so. Arrow functions, const/var, classes, modules - that's all good stuff that has improved the language.
Seemingly on the same side of this argument: I'm also a huge fan of Typescript. It changed how I do large-scale frontend development four or so years ago - static typing in a language helps me write clear code, catches a huge number of bugs at compile-time, and the tooling and overall ecosystem around it is top-notch. Kudos to Microsoft on this one.
But I'm not convinced the value of this proposal nets out positive. The proposal itself says that the primary motivation is to inch JavaScript evolution towards eventually supporting static types:
Does JavaScript need static type-checking?
"Given how much effort organizations and teams have put into building type-checkers and adopting them, the answer is yes."
Why?
You have to accept that JavaScript is the language of the runtime. We shouldn't be caring about types at runtime - that's the job of the toolchain prior to deploying. That's the way every other language works (feel free to tell me how wrong I am in the comments - I am no language expert!).
Static typing only helps developers, not users.
Think about it this way: what level of performance degradation are you willing to accept for full support of static types? Is it ok for the JavaScript parser and runtime to be 5% slower for every web page or 15% larger in code size? Of course I'm pulling these numbers out of my ass.
When I was first fanboying out on TS, I used to think "gee, wouldn't it be really cool for browsers to support Typescript natively". Wouldn't that be an awesome way to learn and view source, etc. But you know what's good for that instead? Super-fast and small JavaScript for best performance and if you want to share your developer brilliance, have an optional compile-mode and source maps that point to your awesome Typescript.
Another thing I don't like about the proposal is that, while they are clearly heavily influenced by Typescript, they hedge:
How does this proposal relate to TypeScript?
"This proposal is a balancing act: trying to be as TypeScript compatible as possible while still allowing other type systems"
You are JavaScript - if you are adamant about eventually supporting static types, why not boldly point towards a "north star" of a language that has already proven itself? Is it that you don't want to admit Typescript won? Is it lingering anti-Microsoft bias?
My opinion: If we are going to add static typing for web apps, we should just use Typescript as a new script type. <script type="application/x-typescript"> seems a better option to me so that the browser can choose an appropriate parser, etc.