Hacker News new | ask | show | jobs
by kinjba11 1556 days ago
> TS already does them?

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';

1 comments

> > 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.

> A JS engine can do this trivially

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.