This is absurd, if you're going to add the annotations at least have the runtime enforce them, otherwise all this is doing is saving a "compile" step for ts users specifically.
It would actually be a net negative for JS, as it would allow the syntax to be shipped on sites, and then because it isn't actually checked at runtime you will inevitably end up with sites if the engines ever do start performing type checks, and so will directly prevent them from adding it later.
So as I understand things:
* This does not actually add type checking
* It does not support the full typescript syntax, so you don't get to skip a compile stage
* This prevents JS engines actually adding type checking later, due to breaking existing content
As the FAQ of the proposal mentions (https://github.com/giltayar/proposal-types-as-comments/#FAQ), we believe adding runtime type checking would be untenable. This stems from both performance concerns and concerns around evolving whatever type-checking would be built in. For that reason, not only do we not expect engines to perform runtime checks, we would specify that these type annotations have no runtime effect.
Yes, and having spent many years working on browsers I am saying that if the annotations have no effect there will be code that ends up depending on the engine not performing those checks. At the point the syntax is burned because enforcing the checks breaks content.
I am unsure why you believe performing the checks is untenable if TS already does them? The runtime checks should be relatively cheap and in perf critical code if it turned out type checks were too expensive there are a bunch of optimizations engines can do, or the impacted functions could drop the annotations.
Adding a significant parsing surface, with the related page load perf impact, with the potential (im going to be generous) to burn the syntax, this seems like nothing but downside.
TS runs the checks at compile time and compiles to JavaScript without annotations. There is no runtime cost.
> related page load perf impact
Type annotations would likely still be stripped by minifiers, just like comments. No change.
> burn the syntax
As a dev that regularly uses TypeScript, being able to use debug builds with annotations and copy paste TypeScript into the console would save a lot of time. This would be part of larger trend of adding type annotations to dynamic languages. Python, Ruby, and other dynamic languages have blazed the trail here. There are ways to add enforcement in the future if needed, just look at 'use strict';
> > TS already does them?
> TS runs the checks at compile time and compiles to JavaScript without annotations. There is no runtime cost.
If there were performance costs this can be mitigated very easily - to be able to do compile time checks means TS is designed largely around early binding, at least in the default case. A JS engine can do this trivially.
> > related page load perf impact
> Type annotations would likely still be stripped by minifiers, just like comments. No change.
You're kidding right? You're proposing permanently changing the language, in a way that burns the type syntax for a feature that you're now saying will be stripped before a JS engine ever sees it?
> As a dev that regularly uses TypeScript, being able to use debug builds with annotations and copy paste TypeScript into the console would save a lot of time.
That sounds like a feature for your console, not the language
> This would be part of larger trend of adding type annotations to dynamic languages. Python, Ruby, and other dynamic languages have blazed the trail here.
Which enforced the types. Understand I am not opposed to the type annotations, I am opposed to this because it does not enforce those types. That means that broken content will exist, and thus burn the syntax.
> There are ways to add enforcement in the future if needed, just look at 'use strict';
Which will never happen again. As the person who implemented strict mode in JSC I can tell you that the cost of a mode switch is massive, in spec and implementation complexity. Adding another mode switch is simply not going to happen. The only reason I was ever ok with adding strict mode to javascript was the removal of the this conversion. No other part of strict mode would have warranted the cost.
I'm not one to argue, computers are fast. However, this would be a huge paradigm shift for the JavaScript ecosystem, where compile-to-JavaScript-tools like TypeScript are dominant. These tools have explicit goals to do type checking at compile time only and avoid generating anything at runtime. Runtime checking is redundant in this paradigm.
Now if you're arguing that the JS engines like V8 should be able to use type information for performance gains, I understand that wish. WebAssembly seems to be taking over the performance angle these days though, as it was designed from the start for that purpose, unlike JavaScript.
> You're kidding right? .. will be stripped before a JS engine ever sees it?
Yes. We're talking JavaScript here - billions of lines of which are compiled every year to ES5 ;-; with polyfills. Throwing away hundreds of useful features of ES2016+. This is par for the course, part of the JavaScript paradigm which this proposal understands.
> sounds like a feature for your console
That is fair, though it seems unlikely Chrome would adopt such a feature for their console without motivation. Perhaps.
> Which enforced the types
Python does not enforce types at runtime? You might be referring to something I'm not familiar with. Fired up Python 3.9. This works fine even though I'm passing strings for int.
def add(a: int, b: int):
return a + b
add("not ", "used") # 'not used'
> the cost of a mode switch is massive
But earlier you said a JS engine can do this trivially? I don't understand how this is now difficult. Ultimately, if the types are enforced, you may as well go the full monty and support TypeScript in the browser, with Deno leading the way. Stop beating the JavaScript horse and all. That's the dream. This proposal is a compromise without a doubt, with the understanding that TypeScript in the browser is politically untenable at this point.
So parsing and enforcing these types would be fine, performance-wise, but having a flag at the start of the module that says "discard types yes/no" would be a massive performance hit?
I really don't understand.
It wouldn't have to support any smaller granularity, and it wouldn't need to change parsing at all.
It would actually be a net negative for JS, as it would allow the syntax to be shipped on sites, and then because it isn't actually checked at runtime you will inevitably end up with sites if the engines ever do start performing type checks, and so will directly prevent them from adding it later.
So as I understand things:
* This does not actually add type checking
* It does not support the full typescript syntax, so you don't get to skip a compile stage
* This prevents JS engines actually adding type checking later, due to breaking existing content