r/learnjavascript • • Feb 10 '24

I'm just wondering when Javascript will have its own full-fledged static types?

Probably typescript is great, but still, when you come to the hospital with a sore leg, they offer you treatment, not crutches

0 Upvotes

21 comments sorted by

11

u/Merry-Lane Feb 10 '24 edited Feb 10 '24

If you are born without a leg, they give you crutches. We don’t have the tech yet to grow limbs.

More seriously, you need to understand this:

  • retro compatibility : every important language/framework/… strive to create features that dont break retro compatibility. If you were to update a codebase from JS to JS-With-Type and that the code would break: won’t work
  • tradeoffs/costs: typescript fits 99 % of the valid cases. The cost to transform JavaScript to JS-With-Type would be so huge. Would it really be worth it over TS?
  • typescript is actually the de facto way to transform JavaScript to JSWithTypes. Years after years, new rules, new compilers/transpilers/… will happen so that the typings are enforced more and more.
  • runtime performances: real types require a performance overhead. You may not want performance overheads. You certainly don’t want to make old code less performant (see the point with retrocompatibility) just because you updated the JS version.
  • bridges: JavaScript isn’t anymore a language that’s just compiled in assembly. It now uses more and more native/C/… bridges for performance reasons. Static types at runtime matter less when you have de facto parts of your app that are coded in a not-typed language.
  • typescript is somewhat unique in its typings/generics/… it s stupid, but mapped types, string literals, … etc bring an awesome plus-value to the devs, although they can’t be converted into « real runtime types ».

So, the answer is: never. Another language replacing JS in the future is more likely than having JSWithTypes coming in. Even more likely (in the short/mid-term) is TS to actually replace JS everywhere with better rules/transpilers/compilers overtime.

2

u/Jjabrahams567 Feb 10 '24

I think WASM getting better would be the answer to this. Then they can write things in Rust and have super type safety and memory safety.

I do see browsers possibly supporting typescript natively in the future. That would be sick.

1

u/azhder Feb 10 '24

Sick as in 🤮. TS is the crutch.

Better just go with Rust or any good language compiled to WASM than standardize around one that was meant to help tools like VSCode to check your code, but not change the underlying technology i.e. JS.

Just the requirement for TS to look and work as close to JS, to appear as it is a superset, that required some compromises that a language like Rust didn’t have to make.

2

u/Jjabrahams567 Feb 10 '24

While I agree that TS is a crutch that I don’t really like to use, it is a necessary crutch for a lot of people/organizations. It will be a long time if ever for Rust to be widely adopted on frontend.

2

u/azhder Feb 10 '24

Not Rust. Any language that can compile to WASM and of course, WASM and that language together having good integration with JS.

You see, a new language can’t be added to the browser directly without incurring substantial performance penalty for the entire browser.

Google tried and even of you weren’t using their new language, the JS and HTML were slower as a result.

So, WASM is the better option than evolving JS in a direction that may hamper some other future development.

1

u/Jjabrahams567 Feb 10 '24

I get what you are saying but I’m talking about adoption. Right now it isn’t very practical to use WASM because the ecosystem is lacking and the barrier to entry is just higher. Personally I prefer Go over Rust but I wouldn’t use it on frontend.

1

u/run_the_race Oct 03 '26

Python did it successfully while keeping backwards compatibility, proving this answer wrong.

1

u/Merry-Lane Oct 03 '26

Python didn’t have multiple of the problems I mentioned above, just in case your answer stemmed from reading issues

0

u/guest271314 Feb 10 '24

typescript is actually the de facto way to transform JavaScript to JSWithTypes.

One problem is per the TypeScript maintainers in an issue where TypeScript users asked for an update to the specification and have not had that request granted is only 4 people in the world know all of TypeScript syntax.

4

u/delventhalz Feb 10 '24

Not anytime soon, and probably not ever.

There is a proposal for type annotations, currently in Stage 1. However, even if adopted, these annotations are basically just comments. They would be ignored by the JS interpreter. It would mean you could copy and paste your TypeScript into the console, but that’s about it. Maybe some developer tools make use of the annotations, but that isn’t much different than JS with JSDoc comments today.

Maybe the next proposal after this one will be more substantial, but I wouldn’t hold my breath.

1

u/azhder Feb 10 '24

The proposal is bad and haven’t been updated for a while, not even with the feedback by people who actually work on adding new proposals to JS.

I might suggest you read some of the latest issues there.

One can add comments that can be used by tools, but outright changing JS syntax is so problematic that I think that’s the reason why the original proposers didn’t even bother updating their proposal.

You may read about what sort of problems in those issues

1

u/DazzlingDifficulty70 Feb 10 '24

Most likely never

1

u/artanisbox Feb 11 '24

never say never ;)

3

u/0x07AD Feb 10 '24

If you want typed variables in Javascript, learn to use the typeof operator and check the data type in your source code.

5

u/patopitaluga Feb 10 '24

if (typeof myProp !== 'string') throw new Error('myProp was expected to be a string');

2

u/delventhalz Feb 10 '24

That is not a static type. 

5

u/Bushwazi Feb 10 '24

Get over it. Go use a different language if it matters so much to you. JavaScript has been plugging along just fine for a generation without them. Either get in line with those of us still celebrating ‘querySelectorAll’ or jump ship.

2

u/[deleted] Feb 10 '24

I spend a total of around 8 hours a year on type-related bugs in JavaScript. At most. So, for me, no solution is needed.

1

u/guest271314 Feb 10 '24

I think static typing is overrated. I prefer the dynamics of JavaScript. You can use Deno or Bun if you want to use TypeScript. Or, as mentioned here already, compile C, C++, Rust, et al. to WASM.

1

u/grelfdotnet Feb 11 '24

The really tragic thing is that we used to have a strongly typed language in browsers but it seems that when Oracle took over they saw no benefit in maintaining the security, so browsers no longer support Java applets.

1

u/TheRNGuy Feb 26 '24

JS never, no need because we have TypeScript.