Skip to content

Instantly share code, notes, and snippets.

@graffhyrum
Last active September 9, 2025 17:52
Show Gist options
  • Select an option

  • Save graffhyrum/a0ef0fa1c4c3eae9703f075b67b457a1 to your computer and use it in GitHub Desktop.

Select an option

Save graffhyrum/a0ef0fa1c4c3eae9703f075b67b457a1 to your computer and use it in GitHub Desktop.
Wrap any arktype type in neverthrow to easily add validation to a callback chain.
import {ArkErrors, type TraversalError, type Type} from 'arktype';
import {err, ok, type Result} from 'neverthrow';
type ArkTypeOut<T> = T extends Type<infer Out, infer _Scope> ? Out : never;
type ArkTypeScope<T> = T extends Type<infer _Out, infer Scope> ? Scope : never;
export function toArkResult<
// biome-ignore lint/suspicious/noExplicitAny: generic function
ArkType extends Type<any,any>,
const Out extends ArkTypeOut<ArkType>,
const Scope extends ArkTypeScope<ArkType>
>(
arkType: ArkType,
input: unknown,
): Result<Out, TraversalError> {
const result = arkType(input);
if (result instanceof ArkErrors) {
return err(result.toTraversalError());
}
return ok(result);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment