Skip to Content
API Reference@parity/resultOverview

@parity/result

a generic, zero-dependency tagged Result type and helpers.

Domain-agnostic: it carries no application-specific concepts, so it can be imported anywhere without cycles.

npm install @parity/result

Exports

Functions

NameSummary
err()Create a failed Result.
isErrorOf()Type-narrowing guard for “is this value an instance of ErrorCtor?”.
normalizeError()Coerce an unknown thrown value into a specific error class, for putting on the
ok()Create a successful Result.
unwrapErr()Assert a Result is err and return its error, throwing otherwise.
unwrapOk()Assert a Result is ok and return its value, throwing otherwise.

Type Aliases

NameSummary
ErrorClassConstructor of an Error subclass that accepts the standard
ResultA value that is either a success (ok) carrying T, or a failure (err) carrying E.

Functions

err()

Create a failed Result.

err(error: E): Result<never, E>

isErrorOf()

Type-narrowing guard for “is this value an instance of ErrorCtor?”.

A thin, typed wrapper over instanceof — useful for narrowing a Result’s err channel (if (isErrorOf(r.error, SomeError))) without a manual cast. Accepts any error class.

isErrorOf(error: unknown, ErrorCtor: (...args: never[]) => E): error is E

normalizeError()

Coerce an unknown thrown value into a specific error class, for putting on the err channel of a Result.

Use this instead of an unchecked error as SomeError cast (which lies to the type system when the thrown value isn’t actually that class). If cause is already an instance of ErrorCtor it’s returned unchanged; otherwise it’s wrapped in a new ErrorCtor whose message is cause’s message (or its stringification) and whose cause is the original value.

normalizeError(cause: unknown, ErrorCtor: ErrorClass<E>): E

Examples

try { risky(); } catch (e) { return err(normalizeError(e, MyError)); }

ok()

Create a successful Result.

ok(value: T): Result<T, never>

unwrapErr()

Assert a Result is err and return its error, throwing otherwise.

unwrapErr(result: Result<unknown, E>): E

unwrapOk()

Assert a Result is ok and return its value, throwing otherwise.

Intended for tests and scripts where an err is a hard failure — the throw surfaces a clear message (and the wrong-channel error) instead of a confusing downstream undefined. Framework-agnostic: it throws a plain Error, so it fails any test runner without depending on one.

unwrapOk(result: Result<T, unknown>): T

Type Aliases

type ErrorClass

Constructor of an Error subclass that accepts the standard (message, options?) shape — the contract an error class must satisfy to be used as the target for normalizeError.

type ErrorClass = (message: string, options?: { cause?: unknown }) => E

type Result

A value that is either a success (ok) carrying T, or a failure (err) carrying E.

type Result = { ok: true; value: T } | { error: E; ok: false }