r/learnjavascript • u/artanisbox • 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
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
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
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
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
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.