
Stop Throwing Exceptions. Use Option and Result Instead.
Let's talk about what's wrong with JavaScript error handling. Here's a function: function getUser ( id : number ): User | null { // ... } The caller has to remember to null-check. The type system nudges them, but there's nothing stopping this: const user = getUser ( 42 ); console . log ( user . name ); // TypeError at runtime if user is null Or this pattern, which is even worse: async function fetchConfig (): Promise < Config > { // can throw network error, parse error, validation error... // none of these appear in the type signature } The errors are invisible. The caller doesn't know what to handle. There's a better model Rust has Option<T> for values that might not exist, and Result<T, E> for operations that can fail. Both are explicit in the type signature . Both force the caller to handle every case. @rslike/std brings this to TypeScript. npm i @rslike/std Option — make absence impossible to ignore import { Some , None , Option , match } from " @rslike/std " ; function findUser (
Continue reading on Dev.to JavaScript
Opens in a new tab

