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
Result or ResultAsync, keep it.
- If the contract requires another shape, the boundary is incompatible.
Treat these as common incompatible boundaries unless the local technology explicitly supports neverthrow returns:
- 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, or Promise of 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
Result or ResultAsync.
When incompatibility exists, keep neverthrow inside the boundary.
- Move business logic into internal helper functions or methods that return
Result or ResultAsync.
- Convert the final
Ok or Err into 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 return neverthrow.
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(...), and errAsync(...) to construct success and failure values explicitly.
Implementation Rules
Do not mark a function or method async if its public contract should be ResultAsync<T, E>.
- Return
ResultAsync directly so callers can compose with .map, .mapErr, .andThen, and .orElse.
- Avoid
Promise<Result<T, E>> unless a framework boundary explicitly requires a native Promise.
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-
neverthrow signatures if they are part of the same requested change.
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
Result or ResultAsync.
- Verify incompatible boundaries adapt from internal
neverthrow results instead of bypassing them.
- Verify imports come from
neverthrow and 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
Result or ResultAsync.
- State which boundaries remained non-
neverthrow and why they were incompatible.
- State how
Err values are typed and mapped.
1---2name: neverthrow-return-types3description: Require `neverthrow`-based return types in TypeScript and JavaScript code whenever the surrounding technology allows it. Use when creating, refactoring, reviewing, or extending standalone functions, exported module functions, class methods, object methods, service methods, repository methods, and similar APIs that should expose explicit success and failure result types in their signatures. Prefer `Result<T, E>` for synchronous code and `ResultAsync<T, E>` for asynchronous code. Only skip a `neverthrow` return type when a framework, library, runtime interface, or externally imposed contract is incompatible and requires a different return shape.4---56# Neverthrow Return Types78## Goal910Model every compatible function and method with `neverthrow` return types.1112This 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.1314Apply this rule to:1516- standalone functions17- exported module functions18- class methods19- object literal methods20- factory-returned methods21- service, repository, and domain APIs2223Do not treat nesting as an exception. A method inside a class or object follows the same rule as a top-level function.2425The only allowed exception is an incompatible boundary enforced by the surrounding technology, library, or framework.2627## Detect Compatibility First28291. Identify the real contract of the function or method before editing.30 - Check implemented interfaces, overridden members, framework callback types, decorators, lifecycle hooks, route handler signatures, component signatures, and public SDK contracts.31 - If the contract already requires `Result` or `ResultAsync`, keep it.32 - If the contract requires another shape, the boundary is incompatible.33342. Treat these as common incompatible boundaries unless the local technology explicitly supports `neverthrow` returns:35 - UI render functions that must return elements or nodes.36 - Framework route handlers, middleware, loaders, actions, controllers, or resolvers that must return `Response`, framework reply objects, `void`, or `Promise` of those values.37 - Event listeners, test callbacks, constructors, getters, setters, and other APIs with fixed runtime signatures.38 - Interface implementations or overridden methods whose declared return type cannot be widened to `Result` or `ResultAsync`.39403. When incompatibility exists, keep `neverthrow` inside the boundary.41 - Move business logic into internal helper functions or methods that return `Result` or `ResultAsync`.42 - Convert the final `Ok` or `Err` into the framework-native return value only at the outermost boundary.43 - Do not fall back to ad hoc failure shapes such as `null`, sentinel values, or framework-specific shortcuts merely because the boundary itself cannot return `neverthrow`.44454. If the target project does not already depend on `neverthrow`, add the dependency only when the task allows dependency changes.46 - If dependency changes are out of scope, state that the policy cannot be fully applied yet.4748## Choose the Return Type49501. Use `Result<T, E>` for synchronous work.512. Use `ResultAsync<T, E>` for asynchronous work.523. Prefer explicit domain error types for `E`.53 - Use discriminated unions, tagged objects, or stable error classes that match the local style.54 - Avoid `unknown`, `any`, and vague string errors unless the codebase already standardizes on them.554. Use `ok(...)`, `err(...)`, `okAsync(...)`, and `errAsync(...)` to construct success and failure values explicitly.5657## Implementation Rules58591. Do not mark a function or method `async` if its public contract should be `ResultAsync<T, E>`.60 - Return `ResultAsync` directly so callers can compose with `.map`, `.mapErr`, `.andThen`, and `.orElse`.61 - Avoid `Promise<Result<T, E>>` unless a framework boundary explicitly requires a native `Promise`.62632. When touching a module, update all edited compatible functions and methods in that scope.64 - Do not convert only top-level functions while leaving neighboring compatible class or object methods on non-`neverthrow` signatures if they are part of the same requested change.6566## Boundary Pattern6768When the outer API cannot return `neverthrow`, use this pattern:6970```ts71function createUser(72 input: CreateUserInput,73): ResultAsync<User, CreateUserError> {74 return validateInput(input).asyncAndThen(insertUser)75}7677export async function post(request: Request): Promise<Response> {78 const result = await createUser(await request.json())7980 return result.match(81 (user) => Response.json(user, { status: 201 }),82 (error) => toErrorResponse(error),83 )84}85```8687## Validate Before Finishing88891. Verify every edited compatible function or method now returns `Result` or `ResultAsync`.902. Verify incompatible boundaries adapt from internal `neverthrow` results instead of bypassing them.913. Verify imports come from `neverthrow` and match actual usage.924. Verify error types are explicit and stable enough for callers.935. Run the normal local validation for the stack when it is safe and in scope, such as tests, linting, or type checks.9495## Report the Outcome9697When finishing the task:9899- State which functions or methods now return `Result` or `ResultAsync`.100- State which boundaries remained non-`neverthrow` and why they were incompatible.101- State how `Err` values are typed and mapped.