← Effect fixes your TypeScript

Effect fixes your TypeScript · Part 2

try/catch is a goto in disguise

· by Tony Tang

catch (e: unknown) throws away every error's name. Effect.catchTag handles errors by name, and the compiler tracks what's left until the error type is never.

Comment below: What's the worst error you ever swallowed?

Effect fixes your TypeScript: a series on the bugs you already ship, and how Effect makes them impossible. All code is real Effect v4 and was compiled before it went on screen. https://effect.website

Transcript

try/catch is a goto in disguise. Watch. getUser throws on line four and execution jumps to a catch block 20 lines away. What lands there? e: unknown. Every error lost its name. So, you write instanceof checks, you forget one, and it gets swallowed or logged as something went wrong. In Effect, errors don't jump, they travel in the type. catchTags. UserNotFound handles exactly that one, and it disappears from the type. Handle DbError, gone. Handle ParseError, and the error type becomes never. Never means nothing left to handle. The compiler checked it, not you. Best practice, handle errors by name and let the compiler track what's left. But all those yield*s, isn't this too FP? Part three, it's just async/await. What's the worst error you ever swallowed?

Full lesson in the Learn Effect course ↗ Distinct failures with Data.TaggedError

More from this series