TypeScript: Pure Functions, Result Objects, No Exceptions
- Published
- Duration
- Language
- English
Summary
A pure function returns the same output for the same input and has no side effects. A function that throws breaks that: it has a second, invisible exit, and nothing in its type tells you it is there. The alternative is to return failure as data, a result object that is either ok true with the data, or ok false with an error. The function parseAge never throws; it returns a status plus the data or the reason it failed, and the error type lists every possible reason. In return, TypeScript will not let you read the data property until you have checked the ok property, and an exhaustive switch over the error union reports any error case you forgot to handle. Libraries still throw: JSON.parse raises a SyntaxError on invalid JSON, and a fetch that fails on the network rejects its promise. Wrap those calls once, at the edge, with an attempt function that runs the call inside try and catch and turns the exception into a result; an async version does the same for promises. You do not need a big rewrite: start with the one function closest to the outside world and make it return a result. Callers that still expect exceptions can call unwrap, which throws the error again, until you convert them too. Bonus: functions that return their errors are easy to test, because you pass an input and compare the output object, with no mocks. Keep throwing for real bugs, and return expected failures as data.
Caveat: the error type of a wrapped library call is unknown on purpose, because a library can throw anything. Result objects do not replace exceptions for real bugs such as a broken invariant.
Every snippet was checked with tsc strict mode on TypeScript 7.0.2 and 5.9.3.
Chapters
- 0:00 Pure functions and the problem with exceptions
- 0:17 Return failure as data: result objects
- 0:42 Wrapping throwing libraries with try and catch
- 0:57 Migrating one function at a time
- 1:11 Testing, and when to still throw
Transcript
Text of the video's narration.
A pure function is simple: same input, same output, no side effects. No hidden state, no surprises. Exceptions break that promise. A function that throws has a second, invisible exit, and nothing in its type tells you it's there.
So make failure part of the return value: ok true with the data, or ok false with an error. Here, parseAge never throws. It returns a status, plus the data or the reason it failed, and the error type lists every possible reason. Now the compiler has your back.
You can't read r.data until you've checked r.ok, and an exhaustive switch flags any error case you forgot to handle. But libraries still throw. JSON.parse does, and so does a failed fetch. So wrap them once, at the edge: attempt runs the call inside a try-catch and turns the exception into a result.
There's an async version for promises. You don't need a big rewrite. Start with one function, the one closest to the outside world, and make it return a result. Callers that still expect exceptions can call unwrap, which throws the error again, until you convert them too.
Bonus: pure functions are trivial to test. Pass an input, compare the output object. No mocks. Keep throwing for real bugs. For expected failures, return them as data.
Sources
- TypeScript handbook, Narrowing (discriminated unions, the never type and exhaustiveness checking) typescriptlang.org
- MDN, JSON.parse (throws SyntaxError if the string is not valid JSON) developer.mozilla.org
- MDN, try...catch developer.mozilla.org
- MDN, fetch (the promise rejects on a network error) developer.mozilla.org
Credits and notes
- Narrated with Phonon; motion in Remotion.



