All files / src result.ts

100% Statements 12/12
100% Branches 0/0
100% Functions 12/12
100% Lines 12/12

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51        4787x     852x       372x       6x       1x         1280x     53x       353x       1x       19x         4787x       1280x    
/** Minimal Result type (neverthrow-compatible subset), kept local so the bundle stays free of transpiled helpers. */
export type Result<T, E> = Ok<T, E> | Err<T, E>;
 
export class Ok<T, E> {
    constructor(readonly value: T) {}
 
    isOk(): this is Ok<T, E> {
        return true;
    }
 
    isErr(): this is Err<T, E> {
        return false;
    }
 
    _unsafeUnwrap(): T {
        return this.value;
    }
 
    _unsafeUnwrapErr(): E {
        throw new Error("Called _unsafeUnwrapErr on an Ok value");
    }
}
 
export class Err<T, E> {
    constructor(readonly error: E) {}
 
    isOk(): this is Ok<T, E> {
        return false;
    }
 
    isErr(): this is Err<T, E> {
        return true;
    }
 
    _unsafeUnwrap(): T {
        throw new Error("Called _unsafeUnwrap on an Err value");
    }
 
    _unsafeUnwrapErr(): E {
        return this.error;
    }
}
 
export function ok<T, E = never>(value: T): Ok<T, E> {
    return new Ok(value);
}
 
export function err<T = never, E = unknown>(error: E): Err<T, E> {
    return new Err(error);
}