import type { Result, Success, Failure } from './types.js' export function isSuccess(result: Result): result is Success { return result[0] === undefined } export function isFailure(result: Result): result is Failure { return result[0] !== undefined } export function success(value: T): Success { return [undefined, value] as const } export function failure(error: E): Failure { return [error, undefined] as const } /** * Helper for exhaustive switch checks on discriminated unions. * If this function is called, it means a case was forgotten in a switch statement. * Use this in the `default` case of switch statements handling tagged errors. * * @param value - The value that should be of type `never` if all cases are handled * @throws {Error} Always throws an error indicating unhandled case * * @example * const DatabaseError = taggedError('DatabaseError') * const NetworkError = taggedError('NetworkError') * type AppError = InstanceType | InstanceType * * function handleError(err: AppError): string { * switch (err._tag) { * case 'DatabaseError': * return `DB: ${err.message}` * case 'NetworkError': * return `NET: ${err.message}` * default: * // TypeScript will error if we forget a case above * return assertNever(err) * } * } */ export function assertNever(value: never): never { throw new Error(`Unhandled case: ${String(value)}`) }