News for hackers. Stuff that matters.
Re:How JavaScript engines achieve great performance by phailhaus on Wednesday November 24, 2021 @03:43PM (#29334894)
Wild how people can have such different experiences. Typescript is hands-down one of my favorite languages, and I would choose its structural type system over Java's any day. I also use Python at work, and it's like night and day in terms of quality. Every day I have to deal with fundamentally unresolvable issues due to Python's half-baked type hints, when I could be writing much more stable, functional, and maintainable Typescript.

Calling it "broken" is complete hyperbole, and no, it's not "worse than Javascript". There's a reason why the entire ecosystem is switching to Typescript, and that's because it is extremely effective at helping teams manage complex stateful applications.

Re:How JavaScript engines achieve great performance by ragnese on Wednesday November 24, 2021 @08:45PM (#29337387)
Well, sure, compared to Python, I guess TypeScript's type system is pretty great...

Its type system is quite broken and I'm not going to walk back on using that term. Sometimes the TypeScript designers/devs will explain that the brokenness is intentional. But some of the issues have nothing to do with JavaScript compatibility. For example, we all know about the conundrum of `type` vs `interface`, but did you know that an object-shaped `type` will automatically conform to a mapped type, but an `interface` wont? Playground: https://www.typescriptlang.org/play?ssl=20&ssc=1&pln=21&pc=1...

Or how about the fact that basic inheritance sub-typing doesn't work?: https://www.typescriptlang.org/play?#code/MYGwhgzhAECCB2BLAt...

Re:How JavaScript engines achieve great performance by zarzavat on Thursday November 25, 2021 @05:20AM (#29339997)
Who cares if TypeScript's type system is unsound if it lets you express things that you cannot express in a nominative system like Java's. I get so much more productivity out of TypeScript's type system than any other language including "serious" type systems like Haskell's.

Moreover TypeScript's type system is so powerful it actually catches a good number of bugs that Haskell would never catch, because it does control flow analysis by default. For many languages you would need to install a separate static analyser (if one even exists), having CFA as part of type checking in your editor is godlike.

Sure if you are writing software for a radiotherapy machine, don't write it in typescript! Use whatever proof assistant takes your fancy. But if bugs are not literally going to kill people, TypeScript's type system is about the best in existence.

Re:How JavaScript engines achieve great performance by saagarjha on Thursday November 25, 2021 @06:08AM (#29340288)
Because type system unsoundness leads to real bugs when people (quite reasonably) make assumptions based on the belief that it is sound. I think on the whole TypeScript does an OK job (considering that most languages have unsound type systems too, including Java) because it’s certainly better than nothing but it’s still something to watch out for.
Re:How JavaScript engines achieve great performance by afiori on Friday November 26, 2021 @04:56AM (#29348838)
It would be nice if typescript had a option to compile with (obviously limited) runtime checks
Re:How JavaScript engines achieve great performance by ragnese on Thursday November 25, 2021 @09:54AM (#29341832)
> Who cares if TypeScript's type system is unsound if it lets you express things that you cannot express in a nominative system like Java's. I get so much more productivity out of TypeScript's type system than any other language including "serious" type systems like Haskell's.

If you enjoy your type system being more expressive at the cost of soundness, you should try this language called JavaScript! It lets you express things that you can't in TypeScript!

> Moreover TypeScript's type system is so powerful it actually catches a good number of bugs that Haskell would never catch, because it does control flow analysis by default. For many languages you would need to install a separate static analyser (if one even exists), having CFA as part of type checking in your editor is godlike.

The control flow analysis has nothing at all to do with the soundness of the type system. Kotlin has similar with it's "smart casting," but that doesn't require it to have an intentionally unsound type system.

Re:How JavaScript engines achieve great performance by epolanski on Thursday November 25, 2021 @08:03PM (#29346606)
I’ve been using typescript for years, and I’ve rarely met these un soundness issues.

I agree that it has its limits, and that there are better type systems, but compared to JavaScript it’s a night and day experience.

The amount of help and clarity it brings on large codebases has no equivalent in JS world, and even tho I love Scala or Elm they are simply not realistic alternatives to JS for most of the industry.

Re:How JavaScript engines achieve great performance by ragnese on Friday November 26, 2021 @12:39PM (#29352221)
There are so many choices, though. I haven't tried any of them myself because I hate JavaScript, I hate front-end stuff, and I hate Node, so I try to avoid all of this.

But I was told that TypeScript was awesome, and the whole industry is moving toward it, so that's what we chose for this project.

Next time I won't choose TypeScript. I'll either stick with vanilla JS and know that I don't have a type system to catch my mistakes, or I'll try one of: Elm, PureScript, Flow, ReScript, ReasonML, Dart, etc.

There are so many choices, and I'm sure the ecosystems for these other choices is absolutely abysmal compared to TypeScript, but I rather reinvent a few wheels than use wheels that look round at first, but aren't.

Re:How JavaScript engines achieve great performance by still_grokking on Thursday November 25, 2021 @09:01PM (#29346877)
Why is Scala not a realistic alternative to JS? It has perfect JS interop.

If TS is an alternative to JS Scala is also. Hard to thing of an argument that doesn't go the same about both languages.

The only real issue is market reach at this point in time.

Re:How JavaScript engines achieve great performance by epolanski on Friday November 26, 2021 @11:45AM (#29351765)
> Why is Scala not a realistic alternative to JS?

- Userbase. Hiring Scala developer is insanely harder than hiring JS developers in most of the world

- Worse ecosystem. This ranges from anything like finding libraries to finding help online.

- Generated bundles and performance are very subpar to most compile-to-js languages.

Re:How JavaScript engines achieve great performance by still_grokking on Saturday November 27, 2021 @06:33PM (#29364684)
Point one and two are basically the same point, and mirror what was subsumed under "market reach".

Point three doesn't sound realistic as you can process compiler output further with the usual JS optimizing tools if you wish. So I would like to see some evidence for the stated claim.

Re:How JavaScript engines achieve great performance by phailhaus on Friday November 26, 2021 @12:02AM (#29347596)
Both your examples are very interesting, in that I can't figure out how they would actually get hurt by these issues when writing code. For your `type` vs `interface` example, is the fear that you won't be able to use a type when a function takes a `Record` type? It's an inconvenience for sure, but that won't inadvertently introduce a bug.

And for your 'inheritance sub-typing' example, can you explain what the "right" behavior would be? It's a function that mutates properties on an object, how is the type system supposed to detect that? That's already a code smell and should be avoided.

Re:How JavaScript engines achieve great performance by ragnese on Friday November 26, 2021 @12:50PM (#29352334)
> For your `type` vs `interface` example, is the fear that you won't be able to use a type when a function takes a `Record` type? It's an inconvenience for sure, but that won't inadvertently introduce a bug.

Right. That one, by itself, won't introduce a bug. But it's just another weird thing to remember about TypeScript's type system being inconsistent. I bumped in to it because I wanted to define my own JSON-like type for use as query params being sent to a particular API that has a custom syntax for certain things (like passing an object as a query param, etc). So I wrote a type something like this:

type ExtendedJson = boolean | number | string | null | undefined | OurCustomTimestampType | SomeOtherCustomTypeWeUse | ExtendedJson[] | ExtendedJsonObject

type ExtendedJsonObject = Record<string, ExtendedJson>

And then in my function that sends requests to the API in question, I require a parameter like `queryParams?: ExtendedJsonObject`, and I have a function that knows exactly how to format this ExtendedJsonObject into the right string for the request. Neato.

Except then, I realized that most of the specific query types I wrote were interfaces. But I only got compile errors in some places and not others. I had no idea why some of my types worked and some didn't. The error message was clear enough, I guess. It said that the type didn't conform to `{ [string]: ExtendedJson }`, but I coudln't figure out why some of my types did conform and some didn't.

What's worse is that some of my query types that were defined as interfaces DID work without a compile error! In hindsight, I now understand that the reason those worked was because I was actually "wrapping" those interfaces into types with things like Readonly<FooQueryParams> or Omit<BarQueryParams, 'someKey'>. So those were now types instead of interfaces, but it wasn't an obvious enough clue for me, so I spent hours trying to figure it all out.

If that were the only weird thing, I wouldn't still be bitching about it, but in the last year that I've been focusing on this frontend project, I feel like I've spent countless hours learning about holes and inconsistencies in TypeScript. It feels like every single week I learn about some other broken or inconsistent nonsense that I just think I shouldn't have to deal with.

> And for your 'inheritance sub-typing' example, can you explain what the "right" behavior would be? It's a function that mutates properties on an object, how is the type system supposed to detect that? That's already a code smell and should be avoided.

The correct behavior is that it should be an error to pass a HasDog to a function that requires a HasAnimal. { pet: Dog } is NOT a sub-type of { pet: Animal } just because Dog is a sub-type of Animal. The type theory concept is called "variance". You have "covariant", "contravariant", and "invariant". Mutable object and array types are invariant in their field types. So you can't pass a Array<Dog> for an Array<Animal> because adding an Animal to an Array<Dog> is a type error: an Animal is not necessarily a Dog. However, you CAN pass an immutable Array<Dog> for an Array<Animal> because you are guaranteed to only be reading the elements as Animals, and a Dog is an Animal. Return types often have the inverse type relationships to input params: You can return an Array<Dog> for an Array<Animal> return type because the caller who receives the returned value will treat it as an Array<Animal>, so it's okay to add a Cat to it, even though it was originally only Dogs.

And, while I agree that mutating an input param is generally a bad idea, that's no excuse for the type system to not be correct. Every other statically typed language is going to handle this correctly (except primitive arrays in Java. They are also unsound, like TS, but, in Java's defense, it's only primitive arrays, and you almost never use arrays directly in Java- you use List<T>, which is not wrong/unsound). When I say every other statically typed language, I'm literally talking about every statically typed language I've ever used: C++, Java (again, except arrays), Go, Swift, Rust, Kotlin, Scala, even PHP. They all handle this correctly, because it's really basic type theory.

Re:How JavaScript engines achieve great performance by phailhaus on Friday November 26, 2021 @07:11PM (#29355718)
> And, while I agree that mutating an input param is generally a bad idea, that's no excuse for the type system to not be correct.

Yes there is: it's more useful. I don't want to have to write a version of my function for every possible subtype. If I'm not mutating properties, then it's more useful for objects and lists to be covariant. It's not "incorrect" for the type system to do so, that is a deliberate design choice that makes functional patterns easier to express.

This is part of what makes TypeScript so great: they made careful tradeoffs given that they were working off of an existing language. JavaScript doesn't really have immutable objects or lists, but invariance would have caused far more problems than it would have solved.

Re:How JavaScript engines achieve great performance by FpUser on Wednesday November 24, 2021 @06:15PM (#29336333)
>"Calling it "broken" is complete hyperbole"

Well, when comparing it with strongly typed languages it does look broken for sure.

Re:How JavaScript engines achieve great performance by masak on Wednesday November 24, 2021 @09:53PM (#29337754)
This reply is a good example of how "strongly typed" ends up meaning practically nothing -- except possibly "the kind of type system I prefer".

I once attended a talk where the speaker had identified half a dozen axes that papers or projects were calling "strong"/"weak" with relation to type systems. Some were not even consistent with themselves, switching definitions halfway.

Type systems are tools, whose formal properties can be described and analyzed in precise detail. Unfortunately, that kind of precision is hard, so semantically empty words like "strong" get used a lot instead. This message is intended to raise awareness about that fact.

Re:How JavaScript engines achieve great performance by FpUser on Wednesday November 24, 2021 @10:01PM (#29337802)
Well one can analyze that type systems are till the hell freezes over. I personally do not care as this precise knowledge (assuming it is formalized and exists) is of zero value to me. When I want to walk I just do. I do not dwell on the details of the walking process.

Anyways most likely you do know well what I meant. JS vs C++ for example.

Re:How JavaScript engines achieve great performance by masak on Thursday November 25, 2021 @10:10PM (#29347191)
Neither the type system of C++ nor the type system of TS is sound, in the sense of "the type system rejects all incorrect programs".

If what you meant by "strong" is just "you can declare types on variables", then both TS and C++ qualify as strong, but JS doesn't.

Your use of the word is based on feeling, not fact. Which was my point.

Re:How JavaScript engines achieve great performance by FpUser on Thursday November 25, 2021 @11:56PM (#29347583)
Typescript compiles down to Javascript. And this is the problem. When I say uint16_t in C++ I know how it will be kept in RAM and what I can do with it, how I can pack things together etc. etc. TS lacks this abilities. You can play with terminology all you want but it does not change simple facts.
Re:How JavaScript engines achieve great performance by kaba0 on Friday November 26, 2021 @04:53AM (#29348825)
You could (probably can) compile C++ to JS. What will that be? In the end, code is just data operating on data with no semantics. Your uint16_t is only meaningful to the compiler, the same way as TypeScript types are (with a few caveat).