Rust-like Error Handling in TypeScript
6 points by csomar
6 points by csomar
TypeScript typing is genuinely hard. It's easy to end up in a codebase where half your types are any and unknown, which defeats the point.
Does this really happen that often? Admittedly I only have my code and my workplace's codebase as measurements, but I can't think of more than a handful times I had to use unknown and practically none where I had to use any.
In TypeScript, you need a lot of self-discipline. If you want to get stuff out there quickly, you can simply slap an any for it. And from there sloppiness starts…
Hmm, our experiences vary wildly then. I tend to prototype by thinking out types and then vary those types a lot as you inevitably need more / different arguments and fields. I never really found this approach slow, as most time is taken by thinking of the algorithm or problem at hand.
I found that using any anywhere compounds really fast and your tooling becomes magnitudes less useful at both catching mistakes and providing hints.
I’ve never had this problem either. Maybe the occasional cast to and from ‘any’ in library code (analogous to unsafe rust), but swaths of untyped code? No
I've seen this with codebases that have been converted to TS from vanilla JS, people will use any as a quick hack to get the transition completed but won't go back and fix the types.