Skip to content

Instantly share code, notes, and snippets.

@vineboneto
Last active August 30, 2024 03:44
Show Gist options
  • Select an option

  • Save vineboneto/236408d584ba79e97b19e13841cfd5cc to your computer and use it in GitHub Desktop.

Select an option

Save vineboneto/236408d584ba79e97b19e13841cfd5cc to your computer and use it in GitHub Desktop.
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