News for hackers. Stuff that matters.
Re:TypeScript Is Surprisingly OK for Compilers by jillesvangurp on Friday August 18, 2023 @04:39AM (#37172850)
I've done a bit of typescript and kotlin-js. It always strikes me how close those two languages are. Yes there are lots of differences but they aren't that different and you can transition from one to the other pretty easily. I have my preferences (kotlin) but I can work with both.

IMHO typescript could just cut loose from its javascript compatibility. Why not compile it to wasm instead of transpiling it to javascript? Kotlin is in the process of adding a wasm compiler and they already have a js transpiler. The same code results in a lot smaller binaries and loads a lot faster in wasm form. Browser Javascript is not a great compilation target. And who cares what the minified crap that loads in the browser looks like? The only reason for backwards compatibility is allowing javascript projects to easily transition to typescript. But that's increasingly less relevant now that a lot of projects start out being typescript from day 1.

Of course, Assembly script is already a thing. But why not make that a bit more official? It wouldn't surprise me to see people doing a lot of web development in languages like that in a few years.

Re:TypeScript Is Surprisingly OK for Compilers by orra on Friday August 18, 2023 @04:54AM (#37172963)
I'm afraid that's wishful thinking. JS compatibility is core to the TypeScript ethos, regardless of TypeScript's own popularity.

Besides, enums (?) aside, and ignoring typechecking, compiling TS is really easy right now. Switching to WASM is a high ask.

Re:TypeScript Is Surprisingly OK for Compilers by horsawlarway on Friday August 18, 2023 @07:55AM (#37174429)
JS compatibility is core to Typescript's own popularity.
Re:TypeScript Is Surprisingly OK for Compilers by flohofwoe on Friday August 18, 2023 @05:32AM (#37173235)
"Modern" Javascript essentially is Typescript without the type hints (e.g. if you ignore the historical baggage, JS is actually a fairly decent language).
Re:TypeScript Is Surprisingly OK for Compilers by junon on Friday August 18, 2023 @05:50AM (#37173395)
I actually argue JS was a better language before all of the changes made, starting with const/let. The only thing I'd say makes sense are classes but the fact they aren't syntax sugar over prototypes was a mistake.

People wanted a different language, they should have gotten more scripting languages in the browser. Not changing JavaScript so much that it's no longer JavaScript.

Re:TypeScript Is Surprisingly OK for Compilers by flohofwoe on Friday August 18, 2023 @05:57AM (#37173456)
Coming into web development fairly recently (after 2013 or so) from the statically typed hemisphere and without closely following the Javascript history from the start I naturally cannot agree ;)

(things like the scoping rules for 'var' is just bizarre, same with the concept of 'prototypes')

Looking at typical "old-school" Javascript code gives me the creeps the same way as looking at K&R C code ;)

Re:TypeScript Is Surprisingly OK for Compilers by shrimpx on Friday August 18, 2023 @06:16AM (#37173617)
> aren't syntax sugar

They aren't syntactic sugar? Pretty sure `class Foo { blah() {} }` is equivalent to `function Foo() {}; Foo.prototype.blah = function() {}`.

Re:TypeScript Is Surprisingly OK for Compilers by bornfreddy on Friday August 18, 2023 @06:20AM (#37173641)
> the fact they aren't syntax sugar over prototypes was a mistake

Totally different meaning.

Re:TypeScript Is Surprisingly OK for Compilers by shrimpx on Friday August 18, 2023 @06:22AM (#37173652)
What else would 'syntax sugar over prototypes' mean?
Re:TypeScript Is Surprisingly OK for Compilers by zdragnar on Friday August 18, 2023 @10:15AM (#37175907)
There are a few very specific differences. In a subclass, 'this' doesn't exist until you call "super" for example, and the constructor will throw an error of invoked without the "new" keyword. [O]

These differences let you extend built-in things that simply can't be done with old prototype constructor syntax. [1]

[O] I should probably be using a more recent reference like MDN, but the 2ality series always sticks out in my mind whenever "just syntax sugar" comes up: https://2ality.com/2015/02/es6-classes-final.html#safety-che...

[1] https://2ality.com/2015/02/es6-classes-final.html#subclassin...

Re:TypeScript Is Surprisingly OK for Compilers by shrimpx on Friday August 18, 2023 @03:58PM (#37181260)
Thanks. It sounds like subclassing built-ins is where transpilers hit a hard wall. Other syntactic features of es6 can be transpiled.
Re:TypeScript Is Surprisingly OK for Compilers by zdragnar on Saturday August 19, 2023 @10:06AM (#37188913)
http://kangax.github.io/compat-table/es6/

This page lists features from es6 (and newer versions linked at the top) along with compliance to the spec. First column is the current browser, second is babel+corejs polyfills.

Overall, babel gets about 70% of the way there.

Re:TypeScript Is Surprisingly OK for Compilers by cxr on Friday August 18, 2023 @10:40AM (#37176302)
[delayed]
Re:TypeScript Is Surprisingly OK for Compilers by junon on Friday August 18, 2023 @11:38AM (#37177122)
They are not, no.
Re:TypeScript Is Surprisingly OK for Compilers by shrimpx on Friday August 18, 2023 @04:00PM (#37181298)
My example is incorrect because it doesn’t cover all the edge cases but you can desugar es6 to es5, except in es5 you can’t subclass builtins.
Re:TypeScript Is Surprisingly OK for Compilers by baq on Friday August 18, 2023 @06:10AM (#37173574)
you can still write the old javascript. it'll feel very lonely, though.
Re:TypeScript Is Surprisingly OK for Compilers by usrusr on Friday August 18, 2023 @07:42AM (#37174344)
Yeah, having just jumped into ts after a long js hiatus since back when The Good Parts was still surprising, it's quite awesome to see how much of what I assumed to be "ts stuff" is actually just postdeluvian js. Makes ts more attractive, not less. I do wonder however of it was possible to identify parts of that language superset that are fully redundant (as in not even required for exotic edge cases) and let loose some draconian linter?
Re:TypeScript Is Surprisingly OK for Compilers by jakubmazanec on Friday August 18, 2023 @09:28AM (#37175319)
Re:TypeScript Is Surprisingly OK for Compilers by usrusr on Friday August 18, 2023 @12:16PM (#37177826)
Sure, but that's still seems quite mix and match, anything goes, choose your own adventure. There are many positives about this approach, but as a learner a little more common ground between codebases would certainly be appreciated.
Re:TypeScript Is Surprisingly OK for Compilers by jakubmazanec on Saturday August 19, 2023 @10:46AM (#37189339)
True. I always thought that we should have additional mode stricter than https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe....
Re:TypeScript Is Surprisingly OK for Compilers by Kyro38 on Friday August 18, 2023 @05:52AM (#37173416)
> IMHO typescript could just cut loose from its javascript compatibility. Why not compile it to wasm instead of transpiling it to javascript

In browsers the Wasm runtime doesn't have access to the DOM APIs. So that's wishful thinking ATM.

Re:TypeScript Is Surprisingly OK for Compilers by jbreckmckye on Friday August 18, 2023 @06:36AM (#37173775)
> IMHO typescript could just cut loose from its javascript compatibility. Why not compile it to wasm instead of transpiling it to javascript?

My fantasy for the past year has been, if I could magically program anything, bringing a compiler and spec wholesale into the world out of the void, I would create a new language (call it WebScript as a placeholder) that

- featured an ML style type system, ADTs, a type syntax nearly indistinguishable from TypeScript

- whose actual core language essentially resembled Kotlin or "Go with exceptions"

- compiled to WASM or JS as a compatibility bridge

Nothing radical. Nothing revolutionary. Just these things would be an immediate plus.

But maybe AssemblyScript is a good enough step towards that.

Re:TypeScript Is Surprisingly OK for Compilers by jitl on Friday August 18, 2023 @07:41AM (#37174331)
Go has exceptions: you can use `panic` to throw/catch just fine. The community will bring out the torch and pitchforks because it’s “not idiomatic” but if you’re programming solo or with other pragmatists don’t let it stop you.
Re:TypeScript Is Surprisingly OK for Compilers by madeofpalk on Friday August 18, 2023 @11:41AM (#37177187)
Or just pass pointers because you need optionality, and get null pointer exceptions for free :)
Re:TypeScript Is Surprisingly OK for Compilers by skybrian on Friday August 18, 2023 @01:02PM (#37178627)
Does the community care if it’s not in a public API? If it’s caught within the same library it doesn’t affect anyone else.
Re:TypeScript Is Surprisingly OK for Compilers by andrewcobby on Friday August 18, 2023 @09:55AM (#37175661)
I’m actually working on a language like this. I quite liked ReScript/ReasonML, but having to manually write binding to use TypeScript or JS code is a drag. I’m making a functional language that looks and feels like TS and lets you import TS directly. Mostly just stripping imperative statements, removing class declaration and adding pattern matching and better data constructors (never liked the discriminated unions). WASM as a target is a bit further off.
Re:TypeScript Is Surprisingly OK for Compilers by srhtftw on Friday August 18, 2023 @03:56PM (#37181227)
I've been hoping to see alternative targets to JS for TS as well. WASM makes a lot of sense but TypeScriptToLua¹ also looks interesting.

1: https://typescripttolua.github.io

Re:TypeScript Is Surprisingly OK for Compilers by AgentME on Friday August 18, 2023 @07:02AM (#37173978)
Typescript is defined to run exactly as the equivalent Javascript you get when all the type declarations are stripped out of the source. There's no benefit in compiling it to WASM unless you had a WASM JS engine (or JS->WASM converter) that executed JS faster than the browser's built-in JS engine (or created WASM binaries that were smaller than compressed minified JS, which seems extra unlikely when you'd have to include a JS runtime). If that existed, browsers would just upgrade to use that internally on all JS.
Re:TypeScript Is Surprisingly OK for Compilers by hibbelig on Friday August 18, 2023 @08:51AM (#37174942)
Your parent suggested to make changes to TS so that TS is no longer compatible with JS (in the sense you described). Once that happens, compiling to WASM instead of transpiling to JS is a very valid design choice.
Re:TypeScript Is Surprisingly OK for Compilers by wiseowise on Friday August 18, 2023 @12:20PM (#37177895)
The whole purpose of TS is better JS.
Re:TypeScript Is Surprisingly OK for Compilers by AgentME on Friday August 18, 2023 @11:09PM (#37185088)
That's fair, though there have been several projects that have attempted to be "JS-ish but with some behavior changes with strict type handling sprinkled in for optimizations" like the Strong types proposal (cancelled) and Dart (which switched to being a compile to JS language just like Typescript), so I'm currently convinced that the trade-offs aren't obviously worth it and that it's unlikely Typescript will change its priorities in that direction in the near future.
Re:TypeScript Is Surprisingly OK for Compilers by 88913527 on Friday August 18, 2023 @03:30PM (#37180884)
You can't strip out enums. They're converted to integers in JavaScript.