Skip to content

Type Alias: Result<Data, Error>

Result<Data, Error> = Ok<Data> | Err<Error>

Defined in: index.ts:74

Represents the result of an operation that can either succeed with data or fail with an error. This is a discriminated union of Ok and Err types where the success property is used as .

Type Parameters

Data

Data = any

The type of the success data

Error

Error = any

The type of the error data

Example

typescript
function divide(a: number, b: number): Result<number, string> {
  if (b === 0) return err("Cannot divide by zero");
  return ok(a / b);
}

const result = divide(10, 2);
if (result.success) {
  console.log(result.data); // 5
} else {
  console.log(result.error); // "Cannot divide by zero"
}