Last active
August 30, 2024 03:44
-
-
Save vineboneto/236408d584ba79e97b19e13841cfd5cc to your computer and use it in GitHub Desktop.
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
| type ErrorBase<R = any> = { | |
| error: string; | |
| info?: R; | |
| [key: string]: any; // Permite outras propriedades opcionais | |
| }; | |
| export type Result<T, R = any> = SuccessResult<T> | FailureResult<R>; | |
| interface SuccessResult<T> { | |
| error: null; | |
| value: T; // T não é null aqui | |
| } | |
| interface FailureResult<R> { | |
| error: ErrorBase<R>; | |
| value: null; | |
| } | |
| export namespace Result { | |
| export function ok<T>(value: T): SuccessResult<T> { | |
| return { error: null, value }; | |
| } | |
| export function fail<R>(error: ErrorBase<R>): FailureResult<R> { | |
| return { error, value: null }; | |
| } | |
| } | |
| export class ValidationError extends Error { | |
| constructor( | |
| message?: string, | |
| public info?: Record<string, any>, | |
| ) { | |
| super(message); | |
| this.name = "ValidationError"; | |
| this.info = info; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment