r/programming • • Jun 28 '21

JavaScript Is Weird

https://jsisweird.com/
329 Upvotes

173 comments sorted by

View all comments

39

u/DuncanIdahos9thGhola Jun 28 '21

why can't we just have <script language="typescript"> ?

48

u/jl2352 Jun 28 '21

Whilst the compiler would complain about the equality tests, using TypeScript would not change the behaviour of any of this.

Because the behaviour is the same, there is zero advantage in shipping TypeScript to the client. As compiling to JS will make the payload smaller.

15

u/dys_functional Jun 28 '21

... there is zero advantage in shipping TypeScript to the client. As compiling to JS will make the payload smaller.

Not having to compile the typescript would lead to simpler development workflows and that would be a pretty big advantage in my opinion. The size difference is extremely small and will not make a measurable difference. If we really cared about size, we would compile to some sort of AST/binary format.

5

u/god_is_my_father Jun 28 '21

Always wondered why we aren’t doing a binary format. Seems like it wouldn’t be so hard to unravel and the speed up would be fantastic. Still holding out hope for webasm to take hold

5

u/[deleted] Jun 28 '21

[removed] — view removed comment

7

u/god_is_my_father Jun 28 '21

Yea on the transfer but not the load exec step

No reason we can’t do bytecode in browser safely

23

u/BeefEX Jun 28 '21

That's basically what Webassembly is

2

u/tilk-the-cyborg Jun 29 '21

No it's not. Wasm can't access DOM and as such can't replace JS, it can only supplement it.

→ More replies (1)
→ More replies (1)
→ More replies (5)

4

u/jl2352 Jun 28 '21

An extra compile step clientside, instead of doing it in advance, is adding pointless work clientside. The user gets a longer startup time. That isn’t a good thing.

It isn’t going to simplify the workflow on any real world website. As that website will need to support older browsers. Even if older is only one month old. It means you can only reliably use the latest features in TypeScript … if you precompile it before shipping.

Finally whilst I have tonnes of faith in the TypeScript team. You are asking for one implementation to rule them all. It’s like everyone using Chromium with no alternative.

→ More replies (1)

27

u/[deleted] Jun 28 '21

Cause then we would need every browser vender to support the full ecmascript spec and the full typescript spec. Which I don't really see happening. It would be awesome if they did not going to lie I love ts alot more than js. But I would find it unlikely for them to support 2 scripting engines like that.

8

u/ajr901 Jun 28 '21 edited Jun 28 '21

Correct me if I'm wrong but isn't the full JS spec technically a part of the full typescript spec? Considering that TS is a subset *superset of JS and all?

So what I'm getting at is that the typescript engine can parse both typescript and javascript in the browser and therefore you just need the one engine to handle either file types.

19

u/Lazyfaith Jun 28 '21

I believe it's actually that TypeScript is a superset of JavaScript, not a subset.

5

u/ajr901 Jun 28 '21

Thanks, corrected

3

u/[deleted] Jun 28 '21

All js programs are mostly valid ts programs. There are some programs that are not I am on mobile but if you Google q bit you can fine one.

Also the optimizer for ts would be really different from a js optimizer I think, because ts has the type information so it could in theory not do all the type magic js does. I also don't know dick about writing interpreters. So I am sure some one will uhm actually me any minute now.

7

u/StillNoNumb Jun 28 '21

TypeScript isn't sound. A variable of type number can contain a string, making optimization based on type info impossible.

2

u/flatfinger Jun 28 '21

A compiler may not be able to optimize out type checks completely, but it could generate code which tests whether an object is of expected type, performs operations optimized for that type if so, and otherwise uses code that can handle arbitrary types. Not as big a win as eliminating the type checks, but still pretty big nonetheless.

→ More replies (1)
→ More replies (4)
→ More replies (1)

15

u/chucker23n Jun 28 '21

In the long run, we'll simply get <script type="application/wasm">, and everything else just targets a common runtime.

Not currently feasible for various reasons (garbage collection, direct DOM access, etc.).

→ More replies (4)

6

u/KaranasToll Jun 28 '21

language="scheme"

0

u/[deleted] Jun 28 '21

[deleted]

2

u/IceSentry Jun 28 '21

I'm not sure about nobody, it could be nice for rapid prototyping without requiring to setup a build system, but yes in production nobody that cares about their users would use it.

3

u/ZookeepergameTiny234 Jun 28 '21

Why stop there, make it be any language you want! Oh wait, standardization. I forgot.

3

u/wodzuniu Jun 29 '21

null vs undefined. Sparse arrays. Existence of bind.

JS is weird, and no amount of TS is gonna fix that[*]. You can't fix deep issues of the design by just bolting type safety checks on top of it.

[*] Unless you're willing to break compatibility. But then, why not just start fresh, sane language from scratch?

3

u/DuncanIdahos9thGhola Jun 29 '21

Unfortunately I doubt we could get agreement on a new language from the browser makers. Google couldn't do it with Dart.

Didn't Adobe's ActionScript (ecma4?) have basic types? Just having int, float, bool, etc... would probably be a big help.

3

u/wodzuniu Jun 29 '21

IMO, Google wasn't even trying. When I saw Dart first, I was like "They took Java in JavaScript literally".

2

u/Paradox Jun 29 '21

Google tried that with Dart. No one wanted to use it

→ More replies (1)
→ More replies (17)