Use when composing or chaining neverthrow Result / ResultAsync values — sequencing multi-step operations where each step can fail, recovering from a specific error, or deciding where to turn a Result into a plain value or HTTP response. Also when reaching for .match mid-pipeline, _unsafeUnwrap, isErr()+.value unwrapping, safeTry / yield*, or a try/catch around Result-returning code.
Ships in typescript-modeling. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
Once a value is a Result/ResultAsync, keep it one. Compose every step with combinators (.andThen / .map / .mapErr / .orElse / .andTee) and call .match exactly once, at the consumption edge, to turn the chain into the outside-world value (HTTP response, CLI exit, rendered output). The error channel stays a typed union the whole way; .match is where — and the only place where — it collapses.
RELATED: [[modeling-errors-as-classes]] owns the error classes and the instanceof edge; [[precise-type-modeling]] owns the shape of the union; [[wrapping-throwing-apis-in-results]] owns the entry point from a throwing API.
| Combinator | Use for |
|---|---|
.andThen(fn) | Next step that can itself fail (returns a Result/ResultAsync). Short-circuits on error. |
.map(fn) | Transform the success value (cannot fail). |
.mapErr(fn) | Normalize the error — e.g. into the shared ApplicationError union so the final .match is exhaustive. |
.orElse(fn) | Recover from an error: return okAsync(fallback) for the case you handle, errAsync(e) to re-propagate the rest. |
.andTee(fn) | Fire a side-effect (notify, log) without changing the value. |
.asyncAndThen(fn) | Bridge a sync Result into an async chain (the first async step). |
fromPromise(p, e => new XError(...)) | Bring a throwing promise into the chain — the one place a throw is caught. |
import { errAsync, okAsync, type ResultAsync } from "neverthrow";
// Each step returns a Result / ResultAsync. Build ONE chain; never unwrap mid-flow.
function run(raw: unknown): ResultAsync<Done, AppError> {
return parse(raw) // Result<Input, AppError>
.asyncAndThen((input) => load(input.id).map((record) => ({ input, record }))) // sync → async
.andThen(({ input, record }) =>
act(record, input).orElse((e) =>
e.code === "recoverable" ? okAsync(fallback) : errAsync(e), // recover one case, re-propagate the rest
),
)
.andTee(() => notify()) // side-effect, value untouched
.andThen((done) => save(done).map(() => done)); // thread `done` past a void step
}
Consume once, at the edge — the only .match:
function handle(raw: unknown): Promise<Response> {
return run(raw).match(
(done) => ({ status: 200, body: done }),
(err) => ({ status: err.statusCode, body: { error: err.error } }),
);
}
.match belongs only where the Result leaves your code for the outside world: the island's hook or the page's <script> (the presenter), a build-time loader, a route handler, a CLI main, or a test assertion (tests may also use isOk()/isErr()). Service/domain/helper layers return the ResultAsync onward — they never .match.
| Instead of | Do |
|---|---|
.match mid-pipeline to branch then re-wrap in ok/err | .andThen (success path) / .orElse (recovery) |
Recovering via match(ok, err => …) then okAsync(...) | .orElse((e) => handled ? okAsync(x) : errAsync(e)) |
_unsafeUnwrap() / _unsafeUnwrapErr() in production code | Carry the Result; collapse only at the edge with .match |
if (r.isErr()) return …; const v = r.value mid-flow | .andThen (isErr/.value is for tests, not production flow) |
try { … } catch around a Result-returning call | fromPromise(p, e => new XError(...)), then chain |
safeTry + yield* generator do-notation | Chain with .andThen / .orElse — this is the project's style |
.match somewhere that is not the consumption edge → use .andThen/.orElse._unsafeUnwrap / _unsafeUnwrapErr outside a test.await a Result and read .value / .error outside a test → keep chaining.import { safeTry } or yield* a Result → use combinators.try/catch wrapping code that already returns a Result → wrap the throwing promise once with fromPromise.What SKILL.md sets; muted values are Claude Code's defaults
Other plugins' skills for the same job
/typescript-modeling:chaining-neverthrow-resultsfrom typescript-modeling
npx claudepluginhub naporin0624/astro-agent-starter --plugin typescript-modeling8 skills in this plugin
/branching-modeled-state-with-switchUse when writing TypeScript that branches on a value carrying a modeled state — a discriminated union, a status/kind/type tag, or any state-machine state — to choose behavior or a return value, or when adding a variant to such a union. Also when reaching for an if/else-if chain, an assertNever helper, or a lookup map to consume a tagged union./explicit-primitive-conversionUse when converting a value to a number, string, or boolean in TypeScript, or annotating a primitive type — about to write Number(x), Number.parseInt, Number.parseFloat, String(x), Boolean(x), unary +x, !!x, .filter(Boolean), .map(Number), or a : Number / : String / : Boolean type annotation./modeling-errors-as-classesUse when defining an error type, an error channel for a Result/ResultAsync, or a failure that crosses a throw / Promise rejection / serialization boundary — anything you would otherwise model as an object union like `{ name: 'XError'; message: string }`./precise-type-modelingUse when authoring or converting TypeScript — defining a type / interface / function signature, typing arguments or state, deciding between optional fields and a union, or about to reach for any, unknown, Record<string, unknown>, an index signature, or an as cast. The minimum modeling rules for making code TypeScript./prefix-match-processorUse when branching keys off the prefix of a string (startsWith, namespaced name families like "file.", "edit.", "edit.image.") and the number of prefix branches is growing, when a prefix if-chain is becoming hard to test or extend per-family, or when adding several new prefix families to an existing dispatcher./switch-patternConvert `if`-based discriminated-value matching (HTTP status codes, event/action `type` fields, mode strings) to exhaustive `switch` statements. Use when refactoring existing code to the project style or when reviewing a file for `if (x === ...) / if (x !== ...)` clusters that should be flattened to switch.