News for hackers. Stuff that matters.

Ask HN: What is the “proper” way to do error-handling in TypeScript?

Folks, I feel like I'm losing my mind. Typescript's concept that `error` is either `any` or `unknown` feels like such a massive gaping problem. I get that it's Javascript's fault for not having a rigorous error typing system. What are some strategies you guys use to expose the kinds of errors your libs throw, or capture the errors other people's code throws?
( 9 comments | 2 points | on HN )
Re:Ask HN: What is the “proper” way to do error-handling in ... by bwestergard on Tuesday January 24, 2023 @01:15PM (#34507561)
On my team we use Rust-style Result/Option types for any unrecoverable exceptions.

https://github.com/supermacro/neverthrow

Re:Ask HN: What is the “proper” way to do error-handling in ... by shams93 on Tuesday January 24, 2023 @12:57PM (#34507305)
catch (e) { const error = e as <Your custom error type>; }
Re:Ask HN: What is the “proper” way to do error-handling in ... by raydiatian on Tuesday January 24, 2023 @01:06PM (#34507423)
On an aside, how do you feel about this? Typescript was supposed to bring type visibility to JavaScript. But this still feels like bring your own research. If you input the wrong <Your custom error type>, TS compiler won’t prompt you. This makes me sad.
Re:Ask HN: What is the “proper” way to do error-handling in ... by raydiatian on Tuesday January 24, 2023 @12:52PM (#34507226)
I have already considered using Errors in return type unions, that is

    function getIntOrFailMiserably(): number | Error {
      ..
    }
Is that really the best there is? Obviously, I can't control OPC (other people's code. you down with OPC? yeah you know me) with this, unless I wrap it and search like a hawk for the error.
Re:Ask HN: What is the “proper” way to do error-handling in ... by TechBro8615 on Tuesday January 24, 2023 @01:09PM (#34507465)
You might like fp-ts and its Result type.
Re:Ask HN: What is the “proper” way to do error-handling in ... by satvikpendem on Tuesday January 24, 2023 @01:48PM (#34508123)
I use fp-ts with its Either type (like Result in other languages like Rust), works great.

https://gcanti.github.io/fp-ts/

Re:Ask HN: What is the “proper” way to do error-handling in ... by raydiatian on Tuesday January 24, 2023 @02:52PM (#34509228)
Had a chat with some of the `typescript` discord folks. Apparently the accepted belief is that “all exceptions are treated as unchecked and there’s not much buyin that exposing the thrown error types add value. Basically it’s “bring your own research” to handing errors, or configure a default catcher.
Re:Ask HN: What is the “proper” way to do error-handling in ... by schwartzworld on Tuesday January 24, 2023 @06:49PM (#34512330)
I created my own npm package with a Result type. https://www.npmjs.com/package/schtate
Re:Ask HN: What is the “proper” way to do error-handling in ... by moystard on Tuesday January 24, 2023 @07:05PM (#34512462)
I usually use the unknown type and leverage type guards for isolating the exact type and processing it. If it falls down the type guards, process as generic error.