import { AsyncResult } from './asyncResult'; import { type ResultBaseError } from './baseResultError'; export type ResultPromise = Promise>; export type ReturnedResultPromise = Promise>; export type ReturnedResult = Ok | Err | Result; export interface Result { readonly value: Val | Err; /** * @description returns result value if it's not an error, otherwise throws an exception * @example * let res = Ok(5); * console.log(res.unwrap()); // 5 * * res = Err('err'); * console.log(res.unwrap()); // Exception: Tried to unwrap an Error result */ unwrap(): Val; /** * @description returns error value if it exists, otherwise throws an exception * @example * let res = Ok(5); * console.log(res.unwrapErr()); // Exception: Tried to unwrap Ok result's error * * res = Err('error msg'); * console.log(res.unwrapErr()); // 'error msg' */ unwrapErr(): Err; /** * @param altVal * @description unwraps original value or replaces it with provided alternative if the result is Err * @example * let res = Ok('great success'); * console.log(res.unwrapOr('not that great')); // prints great success * * res = Err('fatal error'); * console.log(res.unwrapOr('not that great')); // prints not that great */ unwrapOr(altVal: Val): Val; /** * @param altValFactory * @description unwraps original value if the result is Ok, otherwise returns value from factory function * @example * let res = Ok(5); * console.log(res.unwrapOrElse("not a number")); // prints 5 * * res = Err('fatal error'); * console.log(res.unwrapOrElse("not a number")); // prints not a number */ unwrapOrElse(altValFactory: (err: Err) => Val): Val; /** * @param msg * @description unwraps result or throw new Error with message in case of error * @example * let res = Ok(5); * console.log(res.expect('provided message')); // 5 * * res = Err('error msg'); * console.log(res.expect('provided message')); // Exception: provided message */ expect(msg: string): Val; /** * @param msg * @description unwraps error or throws new Error with provided message if the result is Ok * @example * let res = Ok(5); * console.log(res.expectErr('provided message')); // Exception: provided message * * res = Err('error msg'); * console.log(res.expectErr('provided message')); // 'error msg' */ expectErr(msg: string): Err; /** * @description returns true if Result is Ok or false if result is Err * @example * let res = Ok(5); * console.log(res.isOk()); // true * * res = Err('error msg'); * console.log(res.isOk()); // false */ isOk(): this is OK; /** * @description returns true if result is Err, otherwise returns false * @example * let res = Ok(5); * console.log(res.isErr()); // false * * res = Err('error msg'); * console.log(res.isErr()); // true */ isErr(): this is ERR; /** * @description returns value if result is Ok, or undefined if it's Err. * @example * let res = Ok(5); * console.log(res.ok()); // 5 * * res = Err('error msg'); * console.log(res.ok()); // undefined */ ok(): Val | undefined; /** * @description returns error value if result is Err, or undefined if it is Ok. * @example * let res = Ok(5); * console.log(res.err()); // undefined * * res = Err('error msg'); * console.log(res.err()); // 'error msg' */ err(): Err | undefined; /** * @param inspector * @description Will run provided inspector if result is Ok * @example * let res = Ok(5); * res.inspect((val) => console.log(val)); // prints 5 * * res = Err('error msg'); * res.inspect((val) => console.log(val)); // prints nothing */ inspect(inspector: (val: Val) => any): Result; /** * @param inspector * @description will run provided inspector if result is Err * @example * let res = Ok(5); * res.inspect((val) => console.log(val)); // prints noting * * res = Err('error msg'); * res.inspect((val) => console.log(val)); // prints error msg */ inspectErr(inspector: (err: Err) => any): Result; /** * @param mapper * @description returns new result with mapped value. * @example * const res = Ok(5); * const mappedRes = res.map((val) => val + 1); * console.log(mappedRes.unwrap()); // 6 */ map(mapper: (val: Val) => MappedVal): Result; /** * @param mapper * @param fallback * @description returns new result with mapped ok value & mapped error value * @example * const mapOk = (val => val + 1); * const mapErr = (err => err + ' suffix'); * console.log(Ok(5).unwrap()) // 6 * console.log(Err('error msg').unwrapErr()) // error message suffix */ mapOrElse(mapper: (val: Val) => MappedVal, fallback: (err: Err) => MappedVal): Result; /** * @param mapper * @description returns new result with mapped error value. * @example * const res = Err('error msg'); * const mappedRes = res.mapErr(err => err + ' suffix'); * console.log(mappedRes.unwrapErr()); // error msg suffix */ mapErr(mapper: (err: Err) => MappedErr): Result; /** * @description returns new async result constructed from current result * @example * const res = Ok(5); * const asyncRes = res.toAsync(); */ toAsync(): AsyncResult; } declare class ERR implements Result { readonly value: Err; constructor(value: Err); private throwError; private prefixMultiLineStr; private getAdditionalErrorMessage; unwrap(): never; unwrapOr(alt: Res): Res; unwrapOrElse(altValFactory: (err: Err) => Res): Res; expect(msg: string): never; expectErr(_msg: string): Err; unwrapErr(): Err; isOk(): this is OK; isErr(): this is ERR; ok(): undefined; err(): Err; inspect(_inspector: (val: never) => any): Result; inspectErr(inspector: (err: Err) => any): Result; map(_mapper: (val: never) => any): Result; mapOrElse(_mapper: any, fallback: (err: Err) => MappedVal): Result; mapErr(mapper: (err: Err) => MappedErr): Result; toAsync(this: Result): AsyncResult; } declare class OK implements Result { readonly value: Val; constructor(value: Val); unwrap(): Val; unwrapOr(_val: Val): Val; unwrapOrElse(_altValFactory: (err: never) => Val): Val; expect(_msg: string): Val; expectErr(msg: string): never; unwrapErr(): never; isOk(): this is OK; isErr(): this is ERR; ok(): Val; err(): undefined; inspect(inspector: (val: Val) => any): Result; inspectErr(_inspector: (err: never) => any): Result; map(mapper: (val: Val) => MappedVal): Result; mapOrElse(mapper: (val: Val) => MappedVal, _fallback: (err: never) => any): Result; mapErr(_mapper: (err: never) => any): Result; toAsync(this: Result): AsyncResult; } /** * @description Create success option */ export declare function Ok(res: T): OK; /** * @description Create error option */ export declare function Err(err: T): ERR; /** * @description Catches exceptions and converts them into Result * * @example * const res = toResult(() => throw new Error('')); * res.isErr() // true */ export declare function toResult(fn: () => T): Result; /** * @description Creates function returning Result from provided FN returning T and * optional error mapper returning E. If FN throws exception, it is caught and passed to the error mapper. * @example * const rawFn = (a: number) => { * if (a < 0) { * throw new Error('not today') * } * return a; * }; * * const fn = resultify( * rawFn, * err => err.message * ); // (a: number) => Result * * const res = fn(-2); // Result * res.err() // 'not today' */ export declare function resultify(fn: (...params: TParams) => TRes, mapErr?: (err: ResultBaseError) => E): (...params: TParams) => Result; export type Ok = OK; export type Err = ERR; export {}; //# sourceMappingURL=result.d.ts.map