Skip to main content

err

@ts-rust/std


@ts-rust/std / Result / err

Function: err()

Call Signature

function err<T>(error): Result<T, void>;

Defined in: packages/std/src/result/result.ts:97

Creates an Err variant of a Result with a void error.

Wraps undefined in a CheckedError within an Err, indicating a failed outcome with no error value for a checked Result.

Type Parameters

Type ParameterDescription

T

The type of the potential value.

Parameters

ParameterTypeDescription

error

void

The void error (typically omitted or undefined).

Returns

Result<T, void>

A Result containing undefined as Err.

Example

const x = err<number>();

expect(x.isErr()).toBe(true);
expect(x.unwrapErr().expected).toBeUndefined();
expect(x.unwrapErr().unexpected).toBeUndefined();

Call Signature

function err<T, E>(error): Result<T, E>;

Defined in: packages/std/src/result/result.ts:124

Creates an Err variant of a Result containing the given error.

Wraps the provided error in a CheckedError within an Err, indicating a failed outcome for a checked Result. This function accepts raw error value or CheckedError.

  • If called with an error of type E, it creates an ExpectedError variant.
  • If called with a CheckedError, it uses the error as is.

Type Parameters

Type ParameterDescription

T

The type of the potential value.

E

The type of the expected error.

Parameters

ParameterTypeDescription

error

| E | CheckedError<E>

The error to wrap in Err, as a raw E or CheckedError.

Returns

Result<T, E>

A Result containing the error as Err.

Example

const oops = new ResultError("err", ResultErrorKind.Unexpected);
const x = err<number, string>("failure");
const y = err<number, string>(oops);

expect(x.isErr()).toBe(true);
expect(x.unwrapErr().expected).toBe("failure");
expect(y.unwrapErr().unexpected).toBe(oops);