3 ms·
I think another reason they added refinement types in the form of literal string types and literal number types was because there are no data constructors in TS
by leshow 9y ago
I think another reason they added refinement types in the form of literal string types and literal number types was because there are no data constructors in TS & Flow either. This is a bit of speculation on my part.
For example, in lots of strongly typed languages when you make a sum type there's a constructor.
haskell,
data Foo = Baz | Bar
or rust,
enum Foo { Baz, Bar }
Baz and Bar are data constructors, they allow you to pattern match on them and make new values. In ts an equivalent sum type has to use a string/number refinement.
type Foo = { kind: "baz" } | { kind: "bar" }
This fits closer to js semantics and still allows sum types to be written. Unfortunately the pattern matching isn't anywhere near as good, but I applaud them for adding this to the language.