Hacker News new | ask | show | jobs
by rtpg 2332 days ago
Structural typing and actual first class union types in typescript are better than basically all the ML-based languages.

Dumb example: imagine you had

Animal = Dog | Cat

Person = Plumber | Driver

If you want a list of “stuff” in your system , in typescript you just say you have Person | Animal.

In ML languages you would need to introduce an Either type so you’re working with Either Person Animal.

So now in your code you’ll need to introduce a bunch of Lefts and Rights. And if you end up needing to compose you’re introducing even another layer of Lefts and Rights. And you don’t get much of any or the inference capabilities of TS to just do this for you.

ML languages don’t have first class unions, they have ADTs, which introduce difficulties and lead to classic type safety weirdness like “why can’t I write a function that only accepts one ADT variant??”

Typescript resolves a lot of these, do you end up with a much better local maximum for typechecking IMO.

Typescripts type unsoundness means you unfortunately can’t really implement return type polymorphism though...

1 comments

What you are describing is still an ADT. It's just a different syntax.

Indeed, your examples of the TS syntax are more convenient than ML, but that is reversed when going the other way, and decomposing the types into smaller sums instead of composing.