News for hackers. Stuff that matters.
Re:Rewriting TypeScript in Rust? You'D have to be by ajkjk on Wednesday December 14, 2022 @01:39PM (#33987872)
Can't agree. Typescript already has a better type system than a lot of languages in common usage, but it's not good enough and we run into issues all the time because it's not powerful enough. It can probably stop adding features someday, but IMO that day is at least a few years away, when it is actually solving the problem of type safety in web development fully.
Re:Rewriting TypeScript in Rust? You'D have to be by ajkjk on Wednesday December 14, 2022 @01:41PM (#33987919)
In particular, supporting nominal typing, fixing the way index types work, and fixing the story of debugging type failures on giant unions come to mind, but there are countless others. (Don't @ me, my day job is a few versions behind and I can't remember what's in the latest version, but there are plenty of other things if not these.)
Re:Rewriting TypeScript in Rust? You'D have to be by culi on Wednesday December 14, 2022 @02:09PM (#33988465)
Yeah I think the new `satisfies` operator and improvements to type inferences in the latest version likely resolve a lot of your concerns.

TypeScript also supports some really advanced typing I've yet to see in other languages like template literal types (don't @ me, I mostly work with dynamic languages at my current job). I feel like even the TS docs haven't caught up to the full power of TS's full featureset

Re:Rewriting TypeScript in Rust? You'D have to be by svieira on Wednesday December 14, 2022 @03:09PM (#33989601)
FYI, TypeScript already supports nominal types, but the ways in which you "spell" `nominal` are a little unintuitive:

1. Use an enum

2. Use an intersection with an enum and literally anything else (except a number)

3. Use a private field (only works with classes)

4. Use a `declare const FOO: unique symbol` keyed object intersected with anything else (works with everything including numbers, lots of boilerplate to declare a new nominal type).

Re:Rewriting TypeScript in Rust? You'D have to be by ajkjk on Wednesday December 14, 2022 @03:13PM (#33989687)
I'm aware of some of those, but I think they're all suboptimal compared to actual nominal types.

In particular iirc you can't use your nominal/branded type as a map key in a type-safe way.

Re:Rewriting TypeScript in Rust? You'D have to be by scotty79 on Thursday December 15, 2022 @04:39AM (#33997230)
Not sure what you mean:

    enum A {}

    type B = { a: number } & A;

    function newB(i:number) {
      return { a: i } as B;
    }

    let b:B = newB(5);

    let m:Map<B, string> = new Map();

    m.set(b, "test");
    m.set({ a: 2 }, "test"); // errors
Re:Rewriting TypeScript in Rust? You'D have to be by svieira on Thursday December 15, 2022 @11:48AM (#34002223)
What he means is that you didn't _used_ to be able to do this:

    declare const NOMINAL_BRAND: unique symbol;
    enum UserIdType {}
    type UserId = string & {[NOMINAL_BRAND]: UserIdType}
    type UserMap<V> = { [key: UserId]: V };
But at some point in the past few years, this is now supported and things like this work as expected:

    const userId: UserId = "234" as UserId
    const coolUsers: UserMap<boolean> = {}
    coolUsers[userId] = true // works
    coolUsers["isThisOkay"] = false // fails
Re:Rewriting TypeScript in Rust? You'D have to be by ajkjk on Thursday December 15, 2022 @12:40PM (#34002966)
Interesting, I thought that wasn't possible, but now I feel like I'm just confused and mixing it up with something else that wasn't possible but can't remember well enough.

But anyway, what I want from TS is

    type UserId = unique string; // or something
    const userId = "1234" as UserId
and no further boilerplate or trickery to get it to work. Would certainly be nice.
Re:Rewriting TypeScript in Rust? You'D have to be by scotty79 on Thursday December 15, 2022 @08:49PM (#34008543)
You can get close to that:

https://github.com/seancroach/ts-opaque

Re:Rewriting TypeScript in Rust? You'D have to be by no_wizard on Wednesday December 14, 2022 @03:57PM (#33990440)
you could use a WeakMap to emulate private fields too
Re:Rewriting TypeScript in Rust? You'D have to be by the_duke on Wednesday December 14, 2022 @04:46PM (#33991058)
My biggest wish would be proper sum types. The current way of manually creating tagged unions is really awkward.

But that won't be added because there is no native JavaScript equivalent.

Re:Rewriting TypeScript in Rust? You'D have to be by ajkjk on Thursday December 15, 2022 @01:59AM (#33996215)
I wouldn't rule it out. They seem to be increasingly amenable to ergonomic changes that are less coupled to what JS would do in my impression.
Re:Rewriting TypeScript in Rust? You'D have to be by scotty79 on Thursday December 15, 2022 @04:54AM (#33997329)
I think it can be safely ruled out. TypeScript tenet which they consistently uphold is that types can't influence behavior of the running code.

So to differentiate behavior depending of which part of sum type you got you need some value in the "real world" like a tag that can enable you to do that. Because all the type information is gone at runtime.

Strongest violation of this rule I know of are enums that generate their own JS code, but they are their separate thing. Something like sum types would need to be present everywhere.

Honestly I kind of like it this way. I'm trying out Rust right now and I'm very often surprised how what I do in one spot depends on huge context of all the generic types that can create wildly different behavior (mostly huge variety compile type error messages in surprisingly varied and distant spots) depending on what types they infer.

Re:Rewriting TypeScript in Rust? You'D have to be by duped on Wednesday December 14, 2022 @03:30PM (#33990037)
It's also worse in a lot of ways, not due to problems with expressiveness but more soundness and specification. It's pretty easy to make TS keel over during typechecking.

The other issue with the typesystem is the (soon to be obviated) need to compile to decent javascript and interact with it. That made sense before WASM existed and it will make a lot less sense once the component model is stabilized.

Re:Rewriting TypeScript in Rust? You'D have to be by otikik on Wednesday December 14, 2022 @03:48PM (#33990317)
Who is “we” in that phrase? It seems the people I know that use it (from the outside, I don’t do ux) seem content with the current state
Re:Rewriting TypeScript in Rust? You'D have to be by ajkjk on Thursday December 15, 2022 @02:00AM (#33996222)
Ah, 'we' as in me and the people I work with in my day job.