DEV Community

Cover image for Future Javascript: Types may finally be coming to Javascript

Future Javascript: Types may finally be coming to Javascript

Johnny Simpson on April 04, 2022

With the promotion of Type Annotations to Proposal Level 1 Stage, Javascript is one step closer to being a more strongly typed language. Let's dive...
Collapse
 
jonrandy profile image
Jon Randy 🎖️

Those type annotations have no place being processed by the JS engine. That belongs in a browser extension and nowhere else

Collapse
 
ivan_jrmc profile image
Ivan Jeremic

They are not being processed, the engine looks at them same as it looks at // comments which is good because we don't need a transpile step.

Collapse
 
jonrandy profile image
Jon Randy 🎖️ • Edited

So what on earth is the point of them? Or a proposal? Or anything? If they are effectively just 'comments'?

Thread Thread
 
ivan_jrmc profile image
Ivan Jeremic

The same point TypeScript has, Typescript Types are also not used in the JS Engine they help you while Developing.

Thread Thread
 
jonrandy profile image
Jon Randy 🎖️ • Edited

To make the engine ignore the type annotations as if they were comments will require changes to the JS engine (yes, processing). These are changes that only benefit TS developers whilst developing. Adding extra load to the JS engine just for this purpose is plain stupid.

This belongs in a browser extension - which would essentially just be moving a compile-like step into the browser instead of it happening server side.

Let's not let the overblown tooling and complexity that has infected the JS development process in the name of "developer convenience" start dragging down the client side too.

Thread Thread
 
ivan_jrmc profile image
Ivan Jeremic

You clearly have no idea, browser extension wtf? LoL

Thread Thread
 
jonrandy profile image
Jon Randy 🎖️

Yeah - a browser extension to strip out the type annotations before giving the JS to the JS engine. Not sure if feasible, but a way better idea than putting this crap into the core of the engine.

Thread Thread
 
ivan_jrmc profile image
Ivan Jeremic

Are you serious? How do you want to tell millions of people, "Hey please install this Browser Extension" if you really want to strip it out the place to do it is with a build script but not an extension in the browser lol.

Thread Thread
 
jonrandy profile image
Jon Randy 🎖️ • Edited

So now you don't want browsers to accept this syntax? Make your mind up.

If TS developers want this syntax to be accepted in the browser as an aid to their development process (bear in mind that these annotations would be stripped in production code as it makes no sense to bloat the bundles with code that the engine will ignore) then it's not really a big issue for them to have a browser extension to allow it. The majority of people using the browsers are not developers, and this has no benefit to them whatsoever. There are already browser extensions to assist with React development and many other things - how would this be any different?

Unless JS actually implements type hinting using this syntax, then there really is no point modifying the JS engines to allow and ignore it, just to make the lives of a minority of browser users' lives easier.

Thread Thread
 
smpnjn profile image
Johnny Simpson

The idea is that by putting types as comments into Javascript, we create a base for all strongly typed Javascript packages like TypeScript and Flow. That way simple types can be standardised. That's useful in itself, and also acts as a stepping stone to a strongly typed standard version of Javascript.

If we did that in a browser extension, then Javascript files would be invalid if we wrote them with annotated types and didn't have the extension installed.

Thread Thread
 
jonrandy profile image
Jon Randy 🎖️ • Edited

If we did that in a browser extension, then Javascript files would be invalid if we wrote them with annotated types and didn't have the extension installed.

Yes, but this wouldn't be an issue as only developers would be sending this kind of code while they were developing, and they would have the extension installed. Production code would be back to normal JS. Presumably, you'd want to strip comments out of production code anyway... so types in comments, or types as TS syntax really aren't that much different. Swings and roundabouts.

Back to my original question - why the proposal if they're just going to be put in as comments? It would be farcical to suggest any actual implementation of type hinting in JavaScript would have the hints in comments. Also, wouldn't suddenly switching to putting types in comments (instead of the existing TS syntax) really piss off a whole load of TS developers? You'll end up with a mess - some devs doing it the old way, some day it in comments, or some unholy mix of the two.

Thread Thread
 
smpnjn profile image
Johnny Simpson

It's simply because adding types to Javascript wholesale would break half of the internet. It would be a massive language change and would affect many components of Javascript. This change is a slow step in that direction, but it's non-committal. This can be implemented with no damage to already existing websites, and then tc39 can decide how to proceed from there. It's incremental steps.

The main benefit to this is of course it reduces the need to compile. Currently TypeScript requires you to compile your files into standard Javascript so they can be run in the browser or on a Node.JS/Deno server. With this, there is no need to compile anymore. TypeScript files are valid Javascript, the types are just ignored.

Therefore TypeScript can still enforce its rules and checks, and take out the compile step - meaning a faster overall development time for developers.

Thread Thread
 
jonrandy profile image
Jon Randy 🎖️ • Edited

So the proposed change really affects TS then (making the compiler read type info from comments) - complicating development further (as mentioned above)? Honestly, all this faffing around would be gone if developers just accepted JS for the language it is, and work with it instead of fighting against it. I've been coding for 38 years (26 professionally) and - quite honestly, when I first came to JavaScript it was a breath of fresh air after working within the straitjacket of strongly typed languages.

Thread Thread
 
smpnjn profile image
Johnny Simpson

That's why it's annotated, and not enforced. Annotated types mean you can still write your Javascript with types, and it'll be valid Javascript, or you can write it without types, to get the flexibility you describe.

Thread Thread
 
jonrandy profile image
Jon Randy 🎖️ • Edited

So much yak shaving just to avoid adapting your mindset to using a language that doesn't have strict typing. I'll never understand it.

This proposal just feels like TS creators trying to dig themselves out of a hole that they created for themselves

Thread Thread
 
mateusmento profile image
Mateus Sarmento • Edited

Dude you seem a bit stoned reading from your comments. But I don't understand your unnecessary complain with a proposal for types in javascript. To be honest I agree with you to say that the type annotations would not be meaningful for the javascript language itself if "treated like comments", which actually is my complain here. Having type check in runtime would open the doors for something like multiple dispatch for functions which would be wonderful for implementing ad-hoc polymorphism and being able to simply overload functions... I really think you are either being unreasonable or just really stoned. This browser extension thing was hilarious...

 
ivan_jrmc profile image
Ivan Jeremic

If you can do everything in JS there is no need for TS to exist. (Which I hope will happen)

Thread Thread
 
emmanuelthecoder profile image
Emmanuel Aiyenigba

Well, you can't. Or atleast not yet. Let's see what the future holds.

Thread Thread
 
emmanuelthecoder profile image
Emmanuel Aiyenigba

Time shall tell!

Collapse
 
leob profile image
leob

So they're sort of duplicating the work done by Typescript, or what? Now we'll have even more confusion and fragmentation (some people using full TS, others use "JS-the-poor-man's-TS"), or am I misunderstanding things?

Collapse
 
hellojavascriptinfo profile image
HelloJavaScript.info

First I would like to address the elephant in the comments below. Most people who write plain old Vanilla JavaScript have nothing to worry about when it comes to a type system in the browser. You can remain calm about these changes in the browser. Type systems are useful when you are in development mode. You will not see types if you don't use them and if you do you have a much better developer experience. Here is my rule when I am developing an application. If I am working with a team then use types and if I am working by myself it's up to me. For those of you who don't like TypeScript then you should stick to what you know. However, the future use of type systems will become part of your day to day job if you work with large team. Get use to it...

Collapse
 
emmanuelthecoder profile image
Emmanuel Aiyenigba

Typescript has come to stay.

Collapse
 
ivan_jrmc profile image
Ivan Jeremic

Nor really the people behind TS made this proposal.

Thread Thread
 
emmanuelthecoder profile image
Emmanuel Aiyenigba

I know. Types coming to JS is never the end of TS.

Collapse
 
pengeszikra profile image
Peter Vivo

I use hybrid TS / JS application, so if JS also use other type declaration that is will be really confusing. By the way TS type definitions not by accident complex for handle ( near ) all type definition. I think that complexity handle in real time just slow down the browsers.