A typed Result type for error handling without exceptions
Model success and failure as a discriminated union so callers must handle both cases.
- TypeScript
- GreenSnippets
- MIT
- Executed with Node.js 22 (type stripping)
type Ok<T> = { ok: true; value: T };
type Err<E> = { ok: false; error: E };
type Result<T, E = Error> = Ok<T> | Err<E>;
const ok = <T>(value: T): Ok<T> => ({ ok: true, value });
const err = <E>(error: E): Err<E> => ({ ok: false, error });
function parsePort(input: string): Result<number, string> {
const n = Number(input);
if (!Number.isInteger(n) || n < 1 || n > 65535) return err(`invalid port: ${input}`);
return ok(n);
}Example / expected output
parsePort("8080") // { ok: true, value: 8080 }
parsePort("abc") // { ok: false, error: "invalid port: abc" }How it works
Exceptions are invisible in function signatures. A Result<T, E> makes the failure path part of the type, and TypeScript narrows it after you check ok.
Use it at boundaries where failure is expected (parsing, validation, network), and keep exceptions for genuine bugs.
Related snippets
- PHP Tested
Validate emails and URLs in PHP
Use filter_var to validate email addresses and http(s) URLs, with an allow-list for schemes.
- TypeScript Tested
Parse JSON into a typed value with a guard
Safely turn unknown JSON into a typed result by validating its shape instead of casting.
- TypeScript Tested
Typed fetch helper that checks the response
A fetch wrapper that throws on HTTP errors and validates the JSON body with a type guard.
- Python Tested
Dataclass with validation
Validate and normalize fields in __post_init__ so invalid objects can never be created.

