News for hackers. Stuff that matters.
Re:Rewriting TypeScript in Rust? You'D have to be by goto11 on Thursday December 15, 2022 @02:17AM (#33996290)
The event type could just be exposed as a regular property so you could write `element.onclick.add((ev => ...)` so you would get static typing without any need for magic strings.

Magic string are use all over JavaScript for different purposes. For example createElement('P') could just be new PHtmlElement() ...

In other cases magic strings could be replaced with enums.

Re:Rewriting TypeScript in Rust? You'D have to be by madeofpalk on Thursday December 15, 2022 @06:09AM (#33997805)
Changing Javascript is explicitly out of scope for Typescript.

I was curious how other languages did discriminators in a way that makes Typescript's feel alien. Just having different functions doesn't really tackle that.

Re:Rewriting TypeScript in Rust? You'D have to be by 411111111111111 on Thursday December 15, 2022 @10:23AM (#34000891)
Agreed and I think the reason for this confusion is because string types generally aren't considered a complicated feature of typescript.

Nor is it unique to it. I.e. elexir does the same through pattern matching and it works even better as far as I'm concerned.

It's more about the features that enable extended type inference and modifying these inferred types on-the-fly.

Easy to understand type modifiers like Omit<>, Partial<> etc are fine, but if you lean into that you can create pretty hard to understand typing logic. Especially if you're diving into annotations for code generation

Re:Rewriting TypeScript in Rust? You'D have to be by goto11 on Thursday December 15, 2022 @11:10AM (#34001644)
The pattern I'm most familiar with is using type constructors:

   type Shape = Square Int | Circle Int
and then pattern matching:

   print (Square size) = ..
   print (Circle radius) = ...
In Typescript this would be something like:

   type Shape = { type: 'square', size: number } | { type: 'circle', radius: number }
And string matching for type narrowing:

   function print(shape: Shape) {
     if (shape.type === 'square) {
       ...
     } else if (shape.type === 'square) {
      ...
     }
   }
The TypeScript approach is comparatively weird and verbose but also pretty clever since it doesn't require any change to the core language.

It also have some pitfalls, e.g. you can write:

  print({type: 'square', size: 10});
But you cant write:

  const x = {type: 'square', size: 10};
  print(x); <-- type error
You have to write:

  const x = {type: 'square' as const, size: 10};
  print(x);
Things like this just complicates the type system and type inference.