From 510ef8821309fb3aeb543a9daf653107f2fb10a2 Mon Sep 17 00:00:00 2001 From: Mathias Oterhals Myklebust Date: Thu, 5 Sep 2024 13:10:59 +0200 Subject: [PATCH] docs(result): type usage documentation --- studio/utils/result.ts | 45 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/studio/utils/result.ts b/studio/utils/result.ts index 5fb0821b9..a551a07ab 100644 --- a/studio/utils/result.ts +++ b/studio/utils/result.ts @@ -1,9 +1,54 @@ +/** + * Represents the result of an operation that can either succeed or fail. + * + * @template T The type of the successful result value. + * @template E The type of the error in case of failure. + * + * This type is a union of two possible outcomes: + * 1. A successful result: { ok: true, value: T } + * 2. An error result: { ok: false, error: E } + * + * Usage example: + * ```typescript + * type MyResult = Result; + * const successResult: MyResult = { ok: true, value: 42 }; + * const errorResult: MyResult = { ok: false, error: "Operation failed" }; + * ``` + */ export type Result = { ok: true; value: T } | { ok: false; error: E }; +/** + * Creates a successful Result object. + * + * @template T The type of the successful result value. + * @template E The type of the error (not used in this function, but part of the Result type). + * @param value The value to be wrapped in a successful Result. + * @returns A Result object indicating success with the provided value. + * + * Usage example: + * ```typescript + * const result = ResultOk(42); + * // result is { ok: true, value: 42 } + * ``` + */ export function ResultOk(value: T): Result { return { ok: true, value }; } +/** + * Creates an error Result object. + * + * @template T The type of the successful result value (not used in this function, but part of the Result type). + * @template E The type of the error. + * @param error The error to be wrapped in a failed Result. + * @returns A Result object indicating failure with the provided error. + * + * Usage example: + * ```typescript + * const result = ResultError("Operation failed"); + * // result is { ok: false, error: "Operation failed" } + * ``` + */ export function ResultError(error: E): Result { return { ok: false, error }; }