diff --git a/src/types.ts b/src/types.ts index 23420425..fc2edd84 100644 --- a/src/types.ts +++ b/src/types.ts @@ -97,6 +97,72 @@ export type Intersect = (U extends any ? (k: U) => void : never) extends ( */ export type Simplify = {} & { [P in keyof T]: T[P] } +/** + * A result tuple where the error is `undefined`. + * + * @example + * ```ts + * type GoodResult = Ok + * // ^? [undefined, string] + * ``` + */ +export type Ok = [err: undefined, result: TResult] + +/** + * A result tuple where an error is included. + * + * Note that `TError` is non-nullable, which means that + * `Err` and `Err` are not valid. + * + * @example + * ```ts + * type BadResult = Err + * // ^? [Error, undefined] + * + * type BadResult2 = Err + * // ^? [TypeError | MyCoolCustomError, undefined] + * ``` + */ +export type Err = [err: NonNullable, result: undefined] + +/** + * A result tuple. + * + * First index is the error, second index is the result. + * + * @example + * ```ts + * type MyResult = Result + * // ^? Ok | Err + * + * type MyResult2 = Result + * // ^? Ok | Err + * ``` + */ +export type Result = + | Ok + | Err> + +/** + * A promise that resolves to a result tuple. + * + * @example + * ```ts + * type MyResult = ResultPromise + * // ^? Promise | Err> + * + * type MyResult2 = ResultPromise + * // ^? Promise | Err> + * ``` + */ +export type ResultPromise = Promise< + [NonNullable] extends [never] + ? Ok + : [TResult] extends [never] + ? Err> + : Result> +> + /** * Get all properties **not using** the `?:` type operator. */