Skip to content

True Myth / result / Err

Interface: Err<T, E>

An Err instance is the failure variant instance of the Result type, representing a failure outcome from an operation which may fail. For a full discussion, see the module docs.

Extends

  • Omit<ResultImpl<T, E>, "value" | "cast">

Type Parameters

T

T

The type which would be wrapped in an Ok variant of Result.

E

E

The type wrapped in this Err variant of Result.

Properties

error

error: E

The wrapped error value.

Overrides

Omit.error


isErr

isErr: true

Is the Result an Err?

Overrides

Omit.isErr


isOk

isOk: false

Is the Result an Ok?

Overrides

Omit.isOk


variant

readonly variant: "Err"

Err is always Variant.Err.

Overrides

Omit.variant

Methods

and()

and<U>(mAnd): Result<U, E>

Method variant for and

Type Parameters

U

U

Parameters

mAnd

Result<U, E>

Returns

Result<U, E>

Inherited from

Omit.and


andThen()

Call Signature

andThen<U>(andThenFn): Result<U, E>

Method variant for andThen

Type Parameters
U

U

Parameters
andThenFn

(t) => Result<U, E>

Returns

Result<U, E>

Inherited from

Omit.andThen

Call Signature

andThen<R>(andThenFn): Result<OkFor<R>, E | ErrFor<R>>

Method variant for andThen

Type Parameters
R

R extends AnyResult

Parameters
andThenFn

(t) => R

Returns

Result<OkFor<R>, E | ErrFor<R>>

Inherited from

Omit.andThen


ap()

ap<A, B>(this, r): Result<B, E>

Method variant for ap

Type Parameters

A

A

B

B

Parameters

this

Result<(a) => B, E>

r

Result<A, E>

Returns

Result<B, E>

Inherited from

Omit.ap


cast()

cast<U>(): Result<U, E>

Type Parameters

U

U

Returns

Result<U, E>


equals()

equals(comparison): boolean

Method variant for equals

Parameters

comparison

Result<T, E>

Returns

boolean

Inherited from

Omit.equals


flatten()

flatten<A, F>(this): Result<A, E | F>

Given a nested Result, remove one layer of nesting.

For example, given a Result<Result<string, E2>, E1>, the resulting type after using this method will be Result<string, E1 | E2>.

Note

This method only works when the value wrapped in Result is another Result. If you have a Result<string, E> or Result<number, E>, this method won't work. If you have a Result<Result<string, E2>, E1>, then you can call .flatten() to get back a Result<string, E1 | E2>.

Examples

ts
import * as result from 'true-myth/result';

const nested = result.ok(result.ok('hello'));
const flattened = nested.flatten(); // Result<string, never>
console.log(flattened); // Ok('hello')

const nestedError = result.ok(result.err('inner error'));
const flattenedError = nestedError.flatten(); // Result<never, string>
console.log(flattenedError); // Err('inner error')

const errorNested = result.err<Result<string, string>, string>('outer error');
const flattenedOuter = errorNested.flatten(); // Result<string, string>
console.log(flattenedOuter); // Err('outer error')

Type Parameters

A

A

F

F

Parameters

this

Result<Result<A, F>, E>

Returns

Result<A, E | F>

Inherited from

Omit.flatten


inspect()

inspect(fn): Result<T, E>

Run a side effect with the wrapped value without modifying the Result.

This is useful for performing actions like logging, debugging, or other “side effects” external to the wrapped value. (Note: You should never mutate the value in the callback. Doing so will be extremely surprising to callers.) The function is only called if the Result is Ok, and the original Result is returned unchanged for further chaining.

ts
import * as result from 'true-myth/result';

const double = (n: number) => n * 2;
const log = (value: unknown) => console.log(value);

// Logs `42` then `84`, and returns `Ok(84)`.
result.ok<number, string>(42).inspect(log).map(double).inspect(log);

// Does not log anything, and returns `Err('error')`.
result.err<number, string>('error').inspect(log).map(double).inspect(log);

Parameters

fn

(value) => void

The function to call with the wrapped value, only called for Ok.

Returns

Result<T, E>

The original Result, unchanged

Inherited from

Omit.inspect


inspectErr()

inspectErr(fn): Result<T, E>

Run a side effect with a wrapped error value without modifying the Result.

This is useful for performing actions like logging, debugging, or other “side effects” external to the wrapped value. (Note: You should never mutate the value in the callback. Doing so will be extremely surprising to callers.) The function is only called if the Result is Err, and the original Result is returned unchanged for further chaining.

ts
import * as result from 'true-myth/result';

const logError = (error: unknown) => console.logError('Got error:', error);

result.err<number, string>('error')
  .inspectErr((error) => console.log('Got error:', error))
  .mapErr((e) => e.toUpperCase());
// Logs: "Got error: error"
// Returns: Err('ERROR')

result.ok<number, string>(42)
  .inspectErr((error) => console.log('Got error:', error))
  .mapErr((e) => e.toUpperCase());
// Logs nothing
// Returns: Ok(42)

Parameters

fn

(error) => void

The function to call with the error value, only called for Err.

Returns

Result<T, E>

The original Result, unchanged

Inherited from

Omit.inspectErr


map()

map<U>(mapFn): Result<U, E>

Method variant for map

Type Parameters

U

U

Parameters

mapFn

(t) => U

Returns

Result<U, E>

Inherited from

Omit.map


mapErr()

mapErr<F>(mapErrFn): Result<T, F>

Method variant for mapErr

Type Parameters

F

F

Parameters

mapErrFn

(e) => F

Returns

Result<T, F>

Inherited from

Omit.mapErr


mapOr()

mapOr<U>(orU, mapFn): U

Method variant for mapOr

Type Parameters

U

U

Parameters

orU

U

mapFn

(t) => U

Returns

U

Inherited from

Omit.mapOr


mapOrElse()

mapOrElse<U>(orElseFn, mapFn): U

Method variant for mapOrElse

Type Parameters

U

U

Parameters

orElseFn

(err) => U

mapFn

(t) => U

Returns

U

Inherited from

Omit.mapOrElse


match()

match<A>(matcher): A

Method variant for match

Type Parameters

A

A

Parameters

matcher

Matcher<T, E, A>

Returns

A

Inherited from

Omit.match


or()

or<F>(orResult): Result<T, F>

Method variant for or

Type Parameters

F

F

Parameters

orResult

Result<T, F>

Returns

Result<T, F>

Inherited from

Omit.or


orElse()

Call Signature

orElse<F>(orElseFn): Result<T, F>

Method variant for orElse

Type Parameters
F

F

Parameters
orElseFn

(err) => Result<T, F>

Returns

Result<T, F>

Inherited from

Omit.orElse

Call Signature

orElse<R>(orElseFn): Result<T | OkFor<R>, ErrFor<R>>

Method variant for orElse

Type Parameters
R

R extends AnyResult

Parameters
orElseFn

(err) => R

Returns

Result<T | OkFor<R>, ErrFor<R>>

Inherited from

Omit.orElse


toJSON()

toJSON(): Serialized<T, E>

Method variant for toJSON

Returns

Serialized<T, E>

Inherited from

Omit.toJSON


toString()

toString(): string

Method variant for toString

Returns

string

Inherited from

Omit.toString


unwrapOr()

unwrapOr<U>(defaultValue): T | U

Method variant for unwrapOr

Type Parameters

U

U = T

Parameters

defaultValue

U

Returns

T | U

Inherited from

Omit.unwrapOr


unwrapOrElse()

unwrapOrElse<U>(elseFn): T | U

Method variant for unwrapOrElse

Type Parameters

U

U

Parameters

elseFn

(error) => U

Returns

T | U

Inherited from

Omit.unwrapOrElse