About neverthrow-return-types
neverthrow-return-types is a skill for TypeScript development that guides compatible functions and methods toward neverthrow's Result or ResultAsync types. It checks surrounding contracts first and keeps neverthrow handling inside boundaries that require another return shape.
Key features
- 01Checks function and method return contracts
- 02Uses Result for synchronous work
- 03Uses ResultAsync for asynchronous work
- 04Keeps neverthrow results inside incompatible boundaries
- 05Validates error types, imports, and changes
Use cases
- 01Standardize return types across service and domain APIs
- 02Refactor error returns in class or object methods
- 03Adapt internal Result values at framework callbacks
Install
Copy the command and run it in a terminal.
npx skills add https:/ / github.com/ code-sherpas/ agent-skills --skill neverthrow-return-typesAfter installing, type/neverthrow-return-typesin Claude Code to confirm it loaded.
Details
From the project READMENeverthrow 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.