|
|
|
|
|
by debugnik
217 days ago
|
|
First off, the escape hatches in TypeScript are way too accessible compared to OCaml: `JSON.parse(...) as MyInterface` is everywhere and completely broken. And second, you're dismissing the fact that TypeScript is unsound, even worse it is so by design. Easy examples: uninitialized variables holding undefined when their type says they can't [1]; array covariance [2]; and function parameter bivariance, which is part of the TypeScript playground's own example on soundness issues [3] but at least this one can be configured away. C# and Java made the same mistake of array covariance, but they have the decency of checking for it at runtime. [1]: https://www.typescriptlang.org/play/?#code/DYUwLgBAHgXBB2BXA...
[2]: https://www.typescriptlang.org/play/?#code/MYewdgzgLgBAllApg...
[3]: https://www.typescriptlang.org/play/?strictFunctionTypes=fal... As for your example, I agree that TypeScript unions and singleton types are powerful, but I can't see what are you speficially missing here that pattern matching and maybe an additional variant type doesn't get you in OCaml. You get exhaustiveness checking and can narrow the value down to the payload of the variant. |
|
> I can't see what are you speficially missing here that pattern matching and maybe an additional variant type doesn't get you in OCaml. You get exhaustiveness checking and can narrow the value down to the payload of the variant.
A couple things, being constrained to pattern matching in places where imperative programming would be more natural, having to declare lots of special purpose types explicitly vs being able to rely on ad hoc duck typing.