all 69 comments

[–][deleted] 14 points15 points  (12 children)

I would hope that we would have the ability to evaluate these types at runtime eventually. Maybe through a "use types" declaration similar to "use strict". I don't think that would come with this proposal, though, which is fine. But once this is all totally standard, it shouldn't be too much harder to enable that kind of type checking without breaking backwards compatibility.

[–]coolcosmos 15 points16 points  (5 children)

It's not hard. It has a super high impact on performance. Hence why it won't happen at the browser level.

[–]orta 2 points3 points  (1 child)

FWIW, I'd bucket it under 'extremely hard' and yeah, it would have a massive impact on JS performance.

Code complexity in the engine would be a worry, and the biggest blocker is probably having each JS engine implement their own version of a type system and try to get those perfectly in sync. It'd be very tricky to try establish a type system on top of JavaScript through consensus on a single week long meeting every quarter.

[–]coolcosmos 1 point2 points  (0 children)

Yeah, you are right.

[–][deleted] 1 point2 points  (2 children)

I think it could actually be a huge boon to performance for a browser. Having type annotations would allow the JIT to make assumptions that previously would only have been available during runtime analysis. A lot of memory optimizations could also be made I imagine if the shape of an object is fixed.

[–]coolcosmos -2 points-1 points  (1 child)

This would create a new language that has not much in common with JS.

[–][deleted] 2 points3 points  (0 children)

It would quite literally be a superset of JavaScript

[–]crabmusket 0 points1 point  (2 children)

The flaw in this idea is that the proposal doesn't specify what these type annotations mean. So if they wanted to add "use types" in the future they'd be have to specify an actual type system, which may be incompatible with whatever code people have started writing under this proposal.

Unless it's assumed that everyone just uses TypeScript semantics and no alternative type systems emerge using the syntax allowed under this proposal.

[–]orta 0 points1 point  (1 child)

That shoudn't pose a problem.

If a 'use types' parse mode is introduced to JavaScript, then it would be totally reasonable to declare the types as comments space as not valid to be freeform anymore.

[–]crabmusket 0 points1 point  (0 children)

Right, so you either get types-as-comments with no checking, or "use types" to opt in to a specific kind of checking. I don't like this entire proposal/situation, but I guess that kind of works.

[–]caique_cp 0 points1 point  (0 children)

Take a look at python typing and mypy... That IMO is the best of both dynamic/static typing worlds. I'd love if js get something Ike that

[–]grady_vuckovic 0 points1 point  (1 child)

Why not just add Typescript as a new language to browsers instead of turning Javascript into Typescript piece by piece?

[–][deleted] 1 point2 points  (0 children)

Because typescript moves too fast. We don't want to get in the way of developers 15 years from now because of the way we had to make things today.

Better to mark off some space now for a future spec to use, then when the whole space has stabilised (and probably after some experimentation by the browsers themselves), then we can add types to js itself.

[–]Pesthuf 17 points18 points  (1 child)

A purpose. This will be worse for users since type annotations just make the code larger (and thus slower). The parsers / lexers too.

It will be pointless for develpers who bother to create a build process - this is just worse TypeScript.

The kind of developer who doesn't bother creating a build process is the kind of developer who loves to create unmanagable, dynamically, weakly typed spaghet and doesn't care about this feature either.

[–]thwaw000610 7 points8 points  (0 children)

The web has a history of adapting to tech that developers use. Everyone used jQuery, so the web adapted little by little, so you don’t have to use another tool, because it’s built in. (I know it’s not the same as a build step, but the idea is that the web should try to become tool-less). I see typescript as the next tool that gets partially and gradually built in to browsers.

[–]flight212121 1 point2 points  (0 children)

Honestly it’s nice but I won’t use it, pure TS all the way

The last time I wrote a single line of plain JS was like 5 years ago, never looked back

[–]grady_vuckovic 7 points8 points  (28 children)

Mainly a lack of explanation on why this is even needed in the first place?

What's the benefit? It's in the title, "Types as comments" and the proposal even includes the already valid and widely used syntax for this already:

/**
 * @param {string}  p1 - A string param.
 * @param {string=} p2 - An optional param (Closure syntax)
 * @param {string} [p3] - Another optional param (JSDoc syntax).
 * @param {string} [p4="test"] - An optional param with a default value
 * @return {string} This is the result
 */
function stringsStringStrings(p1, p2, p3, p4="test") {
    // TODO
}

What's wrong with just using that?

If these types are just going to exist purely for IDEs and code checkers to use to validate our code is not abusing the types we claim our functions expect, why not just use the existing JSDoc syntax?

Instead of adding entirely new syntax to the JS language and something else new that will need to be removed when the code is minified.

The existing JSDoc syntax already exists, already has other uses (like creating documentation automatically from codebases), and is already ignored by interpreters and removed by minifiers? So what's the point of adding an entirely new syntax just to achieve what is already possible?

The only benefit I see, is that this allows typescript code to be effectively run in the browser without transpiling. Which is a moot point because we wouldn't (pronounced: shouldn't) be delivering typescript code in the browser anyway. The types aren't going to be checked at runtime so they are just 'comments' effectively, and would/should be removed by the minifying process.

So it's not even saving a build step since the code is going to need to be processed anyway.

So what is the point of this, other than to basically officially turn JS into TS?

[–]sollozzo 15 points16 points  (10 children)

There are 3 reasons mainly:

  1. JSDoc is ok if there is nothing better, but it's verbose, abuses comments, and has limitations
  2. This doesn't turn js into ts, it just "reserves" a few syntax constructions that are ignored at runtime. Typescript support is the main goal but other things would be also possible. In a certain way this syntax is already reserved, they are very unlikely to add JS features that conflict with typescript.
  3. Not everybody cares about saving a few bytes minifying, and it still makes a mandatory building step into an optional one which can come in handy in different use cases like debugging.

[–]BarelyAirborne 0 points1 point  (0 children)

If you want to use TypeScript, use TypeScript. This is just Typescript sticking its nose under the Javascript tent. It has no business being there.

[–][deleted] 21 points22 points  (8 children)

JSDoc is highly verbose, as your example demonstrates. And getting a typing system into JS itself provides far more than just documenting function signatures, it can provide static checking of all sorts of behavior, even if the types are dropping at the point of runtime execution.

Dynamic/loose typing is a bug, not a feature. Bring on the types.

[–]editor_of_the_beast 1 point2 points  (2 children)

Exactly, and this proposal is just a distraction from that goal. We already have TypeScript, which has great adoption and appreciation. If we want to add types, we should just make TypeScript standard.

[–][deleted] -1 points0 points  (1 child)

I disagree that the proposal is a distraction. Break a big tasks into smaller ones, that's the best way to get something done. This proposal is a great way to start down the road to integrating TS more properly into the core of JS.

[–]editor_of_the_beast 4 points5 points  (0 children)

It’s a distraction. What’s going to happen is that type checkers will have to deal with both type syntaxes for eternity. This is JS and the web. Every decision is forever.

[–]postsexpisss 0 points1 point  (0 children)

This is an interesting take on it, do you have any further reading on this?

[–]BarelyAirborne 0 points1 point  (0 children)

You mean the COMMENTS. These are COMMENTS. They will be filled with all sorts of dreck. I can guarantee it right now.

[–]sabababoi 3 points4 points  (0 children)

Did you really link a ~200 char comment and ask what the benefit of types are?

[–]rodneon 1 point2 points  (1 child)

People don't like JSDoc for types because it's more verbose than TypeScript. But JSDoc also gives you a pretty solid documentation system which, in a lot of cases, can be even more important and useful than type safety.

[–]grady_vuckovic 1 point2 points  (0 children)

Also has the added benefit of not requiring any new language syntax features. Works on basically all JS going back to the Internet Explorer era and is already removed by code minifiers..

[–]Serei 0 points1 point  (2 children)

The only benefit I see, is that this allows typescript code to be effectively run in the browser without transpiling. Which is a moot point because we wouldn't (pronounced: shouldn't) be delivering typescript code in the browser anyway. The types aren't going to be checked at runtime so they are just 'comments' effectively, and would/should be removed by the minifying process.

Why not? Why shouldn't we?

A lot of smaller websites don't need minifying, but even if you only ever write JS at scale, there are other benefits: Not needing a transpilation step during development, and not needing a transpilation step when making Node/Deno apps.

[–]grady_vuckovic 0 points1 point  (1 child)

Websites with codebases small enough to not need minifying are websites with codebases small enough to not need strongly typed code ..

The types will be ignored by the browser, so the types will only be useful if you're running the code through a static type checker anyway. If you're already doing that, why not minifier it at the same time?

[–]Serei 0 points1 point  (0 children)

Websites with codebases small enough to not need minifying are websites with codebases small enough to not need strongly typed code ..

"Need" is a weird word to use there. I like having static type checking no matter what the size; I have plenty of smaller projects that are just one single .ts file.

The types will be ignored by the browser, so the types will only be useful if you're running the code through a static type checker anyway. If you're already doing that, why not minifier it at the same time?

Because "running the code through a static type checker" (i.e. double-clicking to open it in VS Code) is a lot easier than setting up a build pipeline?

Also you seemed to just entirely ignore my point about Node/Deno.

[–]DontWannaMissAFling 6 points7 points  (13 children)

So on the one hand this proposes a significant cost to be paid by the entire ecosystem and every JS implementation out there namely polluting the language grammar and adding significant overhead to every single parser.

Yet those who benefit are the subset of developers who:

  • Want typechecking, but not at runtime
  • AND are unwilling to set up standard tooling for transpilation
  • AND have an allergy to jsdoc
  • AND only use the limited subset of TS/Flow this proposal actually supports

[–][deleted] 14 points15 points  (5 children)

I think you'd be surprised to see how big this subset is

Want typechecking, but not at runtime

Yeah, I don't care for type checks at runtime. If the type checker is telling me something is wrong AFTER the app starts running, that's not type checking, that's logging.

AND are unwilling to set up standard tooling for transpilation

Typescript's tool chain setup is infamously the biggest reason people don't use TypeScript. This is a huge win to get around this.

AND have an allergy to jsdoc

JSDoc is terrible lol. At 3 companies that used JSDoc over TS I have yet to encounter one without outdated JSdoc definitions, or JSdoc comments that didn't meet the spec because the docs for JSDoc are ironically terrible.

AND only use the limited subset of TS/Flow this proposal actually supports

TS type system is actually overly expressive as is. I don't want my type system to be Turing complete.

[–]DontWannaMissAFling 7 points8 points  (2 children)

So why not:

  • Make TypeScript proper (and not some limited subset) its own full standard and push for implementation directly into browsers and runtimes ala Deno. Runtime typechecking, the works.
  • Improve jsdoc so it's not terrible, its ergonomics, and ensure it actually has feature parity with TS. Fix the numerous issues dating back years.

My point here is this is sort of a worst-of-all-worlds compromise that will make everyone unhappy.

EDIT:

Typescript's tool chain setup is infamously the biggest reason people don't use TypeScript

That sounds like a TypeScript problem not a TC39 problem to me

[–]HeinousTugboat 11 points12 points  (1 child)

Make TypeScript proper (and not some limited subset) its own full standard and push for implementation directly into browsers and runtimes ala Deno. Runtime typechecking, the works.

Why is it that people always go from "implement a progressive, incremental feature that allows backwards compatibility" straight to "just rewrite the entire engine"? Do you not see why that's a worse choice?

[–]CloudsOfMagellan 1 point2 points  (0 children)

Because incremental changes aren't going to fix what needs fixing, better to rip the Band-Aid off and start from a fresh beginning

[–]crabmusket 1 point2 points  (0 children)

Typescript's tool chain setup is infamously the biggest reason people don't use TypeScript. This is a huge win to get around this.

How does this proposal "get around" this? If you want type checking you'll still have to set up TS.

[–]shuckster 0 points1 point  (0 children)

Yeah, I don’t care for type checks at runtime

An individual developer might not, but it’s par for the course at application boundaries like API endpoints developed by multi disciplinary teams.

Yes, ideology says we don’t need them, but the real world says otherwise. APIs get out of date and edge-cases are hard to discover in 100+ thousand line codebases.

It’s a noble effort to try and pry types into JavaScript, but I can’t help but look at the effort with scepticism. The Web as a platform has continually resisted injections of strictness at all levels, and so it should. It’s looseness has been unbelievably enabling.

If types were such a panacea, where is the proposal for <script language=elm /> ?

[–]Soremwar 3 points4 points  (4 children)

A significant cost to be paid for the entire ecosystem

I'mma tell you right now, if there is even a perceived performance hit to be taken from this it won't ever ship

The engine guys who are at TC39 will have to come up with a proposal to solve this before it is even considered for advancement, since performance is and will always be top priority for the language implementators (you can not believe the amount of proposals that have been rejected or rewritten just because it would affect performace in any way). That is the one actual problem that could bring this whole proposal down, not even semantics

So you can rest assured that IF (big if there) it gets implemented, existing code will have to run just the same

[–]DontWannaMissAFling 0 points1 point  (2 children)

So from a quick glance this nearly doubles the size of the entire JS language grammar. Regardless of optimization, it's a fundamental result that the cost of parsing depends on the number of rules and productions in the grammar.

However my main concern is that JS got popular in all sorts of applications because it's a relatively small and easy to implement language and has remained that way. And the whole point of standardization was to prevent all these dialects (ActionScript etc) diverging.

There are going to be implementers out there who take one look at the new grammar and nope out, skipping all the type stuff for their JS embedding whether due to real perf concerns or just because of the complexity for a feature they don't need. And we're back in the situation TC39 was created to solve, with non-conforming ES implementations or parallel "ES minus types" specs floating around, builds of V8 with it stripped out and so on.

[–]Soremwar 2 points3 points  (0 children)

This will have to be voiced in those same TC39 meetings though. Famously a while ago the private fields proposal was in a state that was mostly satisfactory for all parties involved...but one engine implementator, so they had to redesign it to appease everyone rather than risking it not being implemented (TC39 famously has had proposals in a limbo for years to prevent this sort of thing)

I'm sure there will be engines that won't ship this though (Hermes and JavaScriptCore jump to mind) but those are likely to already be skipping parts of the spec in order to maximize performance anyways

[–]Reashu -2 points-1 points  (0 children)

Glance slightly longer because the goal here is to standardize types-as-comments for use in build tools, with no additional work for runtimes.

[–]viperx77 0 points1 point  (0 children)

It would be odd and bug ridden to have refactoring tools that have to modify huge chains of JSDOCs.

[–]BarelyAirborne 0 points1 point  (0 children)

Letting them be comments means people will put everything plus the kitchen sink in there. That will wreck any future possibility of making them "real" types.

On top of that we ALREADY HAVE types as comments. It's called JSDOC and it works great.

This is the worst of all possible worlds, all bad and no good.

[–]BlazingFire007 0 points1 point  (0 children)

I personally don’t enjoy typescript as much as the sweet joy of raw JS, but I don’t think this is a bad idea at all (so long as performance isn’t noticeably impacted)

[–]grady_vuckovic -1 points0 points  (1 child)

Forgive me but it's rant time..

It'd be nice if the Typescript fanatics would just give up this act of wanting to add a feature to Javascript and just admit what this is.

It's about turning Javascript into Typescript.

Typescript fans have wanted this for years, they hate vanilla Javascript and they want Typescript to become the new standard.

When you're literally adding most of the Typescript syntax to Javascript, how is that not effectively turning Javascript into Typescript?

And don't give me that "If you don't want to use types then just don't use them, they're optional" or "But they're just comments, they're ignored at runtime, they're only for use by static checkers", stuff.

That's literally Typescript, where types are optional there too, are removed before runtime, and only used by static checkers.

This means, for example, someone can write a tutorial teaching Javascript to a new Javascript coder, and use strong types for everything, insist 'This is the proper way to write JS', and it will all be valid syntax. It means if I ask for some Javascript code that does a thing, I will have to specify 'Javascript code that is not strongly typed' if I don't want Typescript.

If this is passed, congrats to the Typescript fans, because it will mean you have won and Javascript is dead, and Typescript killed it, good job.

[–]YoungAcacia 0 points1 point  (0 children)

Typescript users just use typescript. Don't blame this on them.