Skip to content

Variable: err()

readonly err: <T, E>(error) => Err<T, E>

Defined in: core/factories.ts:84

Creates an error Result containing an error.

Use when an operation fails and you want to encapsulate the error in a Result instead of throwing an exception.

Type Parameters

T

T = never

Success value type (never used in Err, but needed for typing)

E

E = Error

Error type

Parameters

error

E

Error to encapsulate

Returns

Err<T, E>

An Err Result containing the error

Examples

ts
// With native Error
const result = Result.err(new Error('something went wrong'))
ts
// With string
function validate(age: number): Result<number, string> {
  if (age < 0) return Result.err('age cannot be negative')
  if (age > 150) return Result.err('invalid age')

  return Result.ok(age)
}
ts
// With custom types
type ValidationError = { field: string; message: string }
const result = Result.err<number, ValidationError>({
  field: 'email',
  message: 'invalid format'
})