|
|
|
|
|
by adrusi
2024 days ago
|
|
They're not the same. The difference is that what the article calls "nullable types" are based on commutative unions, while option types are based on non-commutative unions. In haskell, Either a (Either b c) is distinct from Either (Either a b) c. In typescript, a | (b | c) is identical to (a | b) | c. And further, x | x is identical to x in typescript. The upshot is that Haskell (using Either instead of Maybe for clarity) lets you have Either (Either a ()) () as distinct from Either a (), but the equivalent typescript is (a | null) | null, which is equivalent to a | (null | null) which is further equivalent to a | null. So nullable types are less powerful than option types. I'm not certain that they're worse, because dealing with nested option types can get confusing, and maybe making them impossible to express would encourage API design that requires less thinking, but unfortunately I think it's more likely that it would lead to API design with more footguns. |
|
They are differently powerful. Option types nest, while nullable types flatten. But nullable types subtype while option types do not.
Otherwise, I think your comment is an excellent summary of the differences.