Hypothetically, if JS were to implement TS-like typing, what would we gain?

I'm seeing a lot of posts with the "Ideas" tag saying to introduce typing to JS. Frankly, I don't see the point. TypeScript already exists, and when compiling to JavaScript, it's not like the type of a JS var will ever change. TS already made sure of that.

For 25 years, EMCAScript is weakly typed; always has been. At this point, what benefit is there to make a change as drastic as adding types? I get that the run-time is different, but really, is it worth making a gigantic change just to save a couple of microseconds?

I guess my main question would be: What would JS types do, that TS doesn't already do?

ftr, TS doesn't "make sure of that" if the value comes from the network, or the disk, or the user, or third-party code - the types of JS vars change all the time.

2 Likes

Are you talking about that Microsoft article? I saw it earlier today.

One benefit I can think of is that JS types possibly wouldnt require a build/compile step. Small benefit but would be a huge win for projects that want a less complicated setup but still want the typings.

1 Like

The main point of adding types to ES would be to have automatic type validation as part of the language. This would make it easier to catch various types of errors that can occur due to an unexpected value type being assigned to a variable. This would incur a small (possibly negligible) performance hit in any ES engine regardless of the code since every variable write would need to be tested to see if the data being written is compatible. However, that same test written as ES logic is far more expensive.

1 Like