Hacker News new | ask | show | jobs
by nine_k 1362 days ago
The idea is that two types with the same structure aren't always the same thing.

This is easily solved by a tagged type in TS [1], though a bit of syntactic sugar over it would be nice.

[1]: https://kubyshkin.name/posts/newtype-in-typescript/

2 comments

In practice I’m very happy with structural types. It provides all the safety I could wish for and slots in well with the underlying language and broader ecosystem.

Though I do miss an easy way to create something like a type Email of string, as it conforms to a set of rules. But that’s a small price to pay.

Flow has structural typing on types and nominal on classes with correct inheritance semantics, which simply makes sense.

For making Email type distinct from string, you want opaque type (first class in flow) which you can emulate as tagged type in ts.

Oh! Interesting. I was unaware of Flow’s details. Though I never program in an OOP style so I wouldn’t have noticed those nuances anyway.

I know of TS’ opaque types, but it always feels dirty. So here I am, relying on named params instead.

Tagging doesn't solve inheritance problems - object oriented inheritance needs to follow liskov substitution principles, support variance correctly.

Tagging is good for making sum types out of union types, but that's not enough for class inheritance.

Flow does it better by having first class opaque type aliases btw (and having nominal types with oo inheritance support on classes of course).