neverthrow-return-types
Neverthrow Return Types
Goal
Model every compatible function and method with neverthrow return types.
This skill governs return signatures only. If the task also changes how thrown exceptions or rejected promises are captured, use neverthrow-wrap-exceptions alongside this skill.
Apply this rule to:
- standalone functions
- exported module functions
- class methods
- object literal methods
- factory-returned methods
- service, repository, and domain APIs
Do not treat nesting as an exception. A method inside a class or object follows the same rule as a top-level function.
The only allowed exception is an incompatible boundary enforced by the surrounding technology, library, or framework.
Detect Compatibility First
-
Identify the real contract of the function or method before editing.
- Check implemented interfaces, overridden members, framework callback types, decorators, lifecycle hooks, route handler signatures, component signatures, and public SDK contracts.
- If the contract already requires
ResultorResultAsync, keep it. - If the contract requires another shape, the boundary is incompatible.
-
Treat these as common incompatible boundaries unless the local technology explicitly supports
neverthrowreturns:- UI render functions that must return elements or nodes.
- Framework route handlers, middleware, loaders, actions, controllers, or resolvers that must return
Response, framework reply objects,void, orPromiseof those values. - Event listeners, test callbacks, constructors, getters, setters, and other APIs with fixed runtime signatures.
- Interface implementations or overridden methods whose declared return type cannot be widened to
ResultorResultAsync.
-
When incompatibility exists, keep
neverthrowinside the boundary.- Move business logic into internal helper functions or methods that return
ResultorResultAsync. - Convert the final
OkorErrinto the framework-native return value only at the outermost boundary. - Do not fall back to ad hoc failure shapes such as
null, sentinel values, or framework-specific shortcuts merely because the boundary itself cannot returnneverthrow.
- Move business logic into internal helper functions or methods that return
-
If the target project does not already depend on
neverthrow, add the dependency only when the task allows dependency changes.- If dependency changes are out of scope, state that the policy cannot be fully applied yet.
Choose the Return Type
- Use
Result<T, E>for synchronous work. - Use
ResultAsync<T, E>for asynchronous work. - Prefer explicit domain error types for
E.- Use discriminated unions, tagged objects, or stable error classes that match the local style.
- Avoid
unknown,any, and vague string errors unless the codebase already standardizes on them.
- Use
ok(...),err(...),okAsync(...), anderrAsync(...)to construct success and failure values explicitly.
Implementation Rules
-
Do not mark a function or method
asyncif its public contract should beResultAsync<T, E>.- Return
ResultAsyncdirectly so callers can compose with.map,.mapErr,.andThen, and.orElse. - Avoid
Promise<Result<T, E>>unless a framework boundary explicitly requires a nativePromise.
- Return
-
When touching a module, update all edited compatible functions and methods in that scope.
- Do not convert only top-level functions while leaving neighboring compatible class or object methods on non-
neverthrowsignatures if they are part of the same requested change.
- Do not convert only top-level functions while leaving neighboring compatible class or object methods on non-
Boundary Pattern
When the outer API cannot return neverthrow, use this pattern:
function createUser(
input: CreateUserInput,
): ResultAsync<User, CreateUserError> {
return validateInput(input).asyncAndThen(insertUser)
}
export async function post(request: Request): Promise<Response> {
const result = await createUser(await request.json())
return result.match(
(user) => Response.json(user, { status: 201 }),
(error) => toErrorResponse(error),
)
}
Validate Before Finishing
- Verify every edited compatible function or method now returns
ResultorResultAsync. - Verify incompatible boundaries adapt from internal
neverthrowresults instead of bypassing them. - Verify imports come from
neverthrowand match actual usage. - Verify error types are explicit and stable enough for callers.
- Run the normal local validation for the stack when it is safe and in scope, such as tests, linting, or type checks.
Report the Outcome
When finishing the task:
- State which functions or methods now return
ResultorResultAsync. - State which boundaries remained non-
neverthrowand why they were incompatible. - State how
Errvalues are typed and mapped.
More from code-sherpas/agent-skills
neverthrow-wrap-exceptions
Capture exceptions and promise failures with `neverthrow` instead of hand-written `try/catch` in TypeScript and JavaScript code. Use when wrapping synchronous functions that may throw, promise-returning functions that may throw before returning, existing `PromiseLike` values that may reject, or third-party APIs such as parsers, database clients, HTTP clients, file-system helpers, serializers, and SDK calls. Prefer `Result.fromThrowable` for synchronous throwers, `ResultAsync.fromThrowable` for promise-returning functions that may throw or reject, and `ResultAsync.fromPromise` when you already have a `PromiseLike` value in hand. Only keep `try/catch` when the language construct, cleanup requirement, or framework boundary truly requires it.
11atomic-design
Create or update web UI components with a strict reuse-first workflow. Use when building, refactoring, restyling, or extending frontend or template components while minimizing raw DOM or HTML by reusing or generalizing existing components first.
10write-persistence-representations
Create or update persistence-layer data representations in any stack, including ORM entities, schema definitions, table mappings, document models, collection definitions, and similar database-facing code. Use when agents needs to add or change persisted fields, identifiers, relationships, indexes, timestamps, auditing fields, or storage mappings in frameworks, libraries, or ORMs such as Prisma, TypeORM, Sequelize, Drizzle, Mongoose, Hibernate/JPA, Doctrine, Ecto, Active Record, or equivalent persistence technologies.
7business-logic
Identify, interpret, review, or write business logic in code. Use when an agent needs to decide whether code expresses business rules, business algorithms, or business workflows, or when it must implement, preserve, or refactor code that creates, stores, or transforms data according to real business policies.
7immutable-domain-entities
Require the immutable design pattern for domain entities. Use when an agent needs to create, modify, review, or interpret domain entities and should preserve identity while expressing state changes through new immutable instances. Domain entities must be modeled as immutable classes, not as plain type aliases or interfaces paired with standalone functions.
7update-agent-skills
Update agent skills installed with the `skills` CLI. Use when asked to refresh installed skills, keep a project's skills current, or troubleshoot cases where `npx skills update` reports that everything is up to date. For project-scoped installs, a no-change update must immediately run the bundled reinstall script so tracked skills from `skills-lock.json` are reinstalled without extra investigation.
7