4 ms·
> There is no other programming language that has union types as powerful as TypeScript How about Swift and OCaml? ReasonML comes to mind too. Haven't tried ac
by ElFitz 7y ago
> There is no other programming language that has union types as powerful as TypeScript
How about Swift and OCaml? ReasonML comes to mind too. Haven't tried actively comparing any of them though.
- tomp 7y agoOCaml/Reason have discriminated unions, which means that you have to explicitly specify which variant of the union you're using, such as type union = Bool of bool | Char of char | Nothing let (x, y, z) = (Nothing, Bool true, Char 'x')
- saagarjha 7y agoIs this not what enums with associated values are?
- tomp 7y agoProbably... but in any case TypeScript also supports naked (edit: I just made up the name) unions, where you don't need to declare them upfront, and where values of different types can be included directly. Such as: let a = random() ? 'a' : 1 TypeScript infers type string | number
- marton78 7y agoThis is still a discriminated union, only that the discriminator is provided by Javascript and is thus hidden: it's `typeof`.
- tomp 7y agoYeah, that's the runtime perspective... but for the type system, they're different... with the "naked" union being much harder (as they're non-algebraic - `a | b | b` always equals `a | b` and might even equal just `a` for some values of `a` and `b`)
- spion 7y agoNo, this works on any overlapping types too - even unsound combinations such as `T | Array<T>`
- ElFitz 7y agoInteresting! Thanks!
- kbp 7y agoWhat features are you thinking of in those languages? I don't know Swift but I can't think of anything comparable in OCaml.
- ElFitz 7y agoSorry; I don't have much of an answer here. I merely know from experience that they have a pretty good type system, but not much more. So I asked :-) Answers have been enlightening