Last active
September 9, 2025 17:52
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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