Skip to content

Interface: Err<TValue, TError>

Defined in: src/types/index.ts:42

Failure variant of Result.

See

Ok - for the success variant

Example

ts
const result = Result.err('failed')

result.isErr()     // => true
result.unwrapErr() // => Err("failed")

Extends

Type Parameters

Type ParameterDefault typeDescription
TValueneverSuccess type carried by Result. Present only for union compatibility.
TErrorErrorType of the stored error.

Type Guards

isErr()

isErr(): this is Err<TValue, TError>

Defined in: src/types/methods.ts:74

Returns true when this result is the Err variant.

Returns

this is Err<TValue, TError>

See

isOk - Opposite check.

Example

ts
Result.err('failed').isErr() // => true
Result.ok(42).isErr()        // => false

Inherited from

ResultMethods.isErr


isErrAnd()

isErrAnd(condition): this is Err<TValue, TError>

Defined in: src/types/methods.ts:105

Returns true when this result is Err and its error satisfies the provided predicate.

Parameters

ParameterTypeDescription
condition(error) => booleanPredicate used to validate the error value.

Returns

this is Err<TValue, TError>

See

isOkAnd - Opposite check for success values.

Example

ts
Result.err('timeout').isErrAnd((e) => e === 'timeout') // => true
Result.ok(42).isErrAnd(() => true)                     // => false

Inherited from

ResultMethods.isErrAnd


isOk()

isOk(): this is Ok<TValue, TError>

Defined in: src/types/methods.ts:61

Returns true when this result is the Ok variant.

Returns

this is Ok<TValue, TError>

See

isErr - Opposite check.

Example

ts
Result.ok(42).isOk()        // => true
Result.err('failed').isOk() // => false

Inherited from

ResultMethods.isOk


isOkAnd()

isOkAnd(condition): this is Ok<TValue, TError>

Defined in: src/types/methods.ts:90

Returns true when this result is Ok and its value satisfies the provided predicate.

Parameters

ParameterTypeDescription
condition(value) => booleanPredicate used to validate the success value.

Returns

this is Ok<TValue, TError>

See

isErrAnd - Opposite check for errors.

Example

ts
Result.ok(42).isOkAnd((x) => x > 40)   // => true
Result.ok(10).isOkAnd((x) => x > 40)   // => false
Result.err('fail').isOkAnd(() => true) // => false

Inherited from

ResultMethods.isOkAnd

Alternation

and()

and<U, E2>(other): Result<U, TError | E2>

Defined in: src/types/methods.ts:432

Returns other when this result is Ok.

If this result is Err, the current error is preserved.

Commonly used to sequence results while discarding the current success value.

Type Parameters

Type ParameterDefault typeDescription
U-Next success value type.
E2neverNext error type.

Parameters

ParameterTypeDescription
otherResult<U, E2>Result returned when this result is Ok.

Returns

Result<U, TError | E2>

See

Example

ts
Result.ok(1).and(Result.ok(2))        // => Ok(2)
Result.ok(1).and(Result.err('fail'))  // => Err("fail")
Result.err('fail').and(Result.ok(42)) // => Err("fail")

Inherited from

ResultMethods.and


andThen()

andThen<U, E2>(next): Result<U, TError | E2>

Defined in: src/types/methods.ts:462

Chains a function that returns another Result.

If this result is Ok, next receives the success value. If this result is Err, the current error is preserved.

Also known as flatMap or bind.

Type Parameters

Type ParameterDefault typeDescription
U-Next success value type.
E2neverNext error type.

Parameters

ParameterTypeDescription
next(value) => Result<U, E2>Function that returns the next result.

Returns

Result<U, TError | E2>

See

Example

ts
Result.ok(5).andThen((x) => Result.ok(x * 2))
// => Ok(10)

Result.ok(5).andThen(() => Result.err('failure'))
// => Err("failure")

Result.err('fail').andThen((x) => Result.ok(x * 2))
// => Err("fail")

Inherited from

ResultMethods.andThen


or()

or<U, E2>(other): Result<TValue | U, E2>

Defined in: src/types/methods.ts:483

Returns this result when it is Ok, otherwise returns other.

Commonly used to provide a fallback result.

Type Parameters

Type ParameterDefault typeDescription
UTValueAlternative success type
E2neverAlternative error type

Parameters

ParameterTypeDescription
otherResult<U, E2>Result used when this result is Err.

Returns

Result<TValue | U, E2>

See

Example

ts
Result.ok(1).or(Result.ok(2))        // => Ok(1)
Result.err('fail').or(Result.ok(42)) // => Ok(42)

Inherited from

ResultMethods.or


orElse()

orElse<U, E2>(fallback): Result<TValue | U, E2>

Defined in: src/types/methods.ts:511

Returns this result when it is Ok, otherwise invokes fallback with the current error.

Useful for recovery flows that depend on the failure reason.

Type Parameters

Type ParameterDefault typeDescription
UTValueRecovery success type
E2neverRecovery error type

Parameters

ParameterTypeDescription
fallback(error) => Result<U, E2>Function used to recover from an error.

Returns

Result<TValue | U, E2>

See

Example

ts
Result.ok(42).orElse(() => Result.ok(0))
// => Ok(42)

Result.err('not found').orElse(() => Result.ok(null))
// => Ok(null)

Result.err('fail').orElse(() => Result.err('backup'))
// => Err("backup")

Inherited from

ResultMethods.orElse

Async Alternation

andAsync()

andAsync<U, E2>(other): AsyncResult<U, TError | E2>

Defined in: src/types/methods.ts:828

Returns other when this result is Ok.

If this result is Err, the current error is preserved.

Type Parameters

Type ParameterDefault typeDescription
U-Next success value type.
E2neverNext error type.

Parameters

ParameterTypeDescription
otherAsyncResult<U, E2>Async result returned when this result is Ok.

Returns

AsyncResult<U, TError | E2>

See

and - Sync version

Example

ts
await Result.ok(5).andAsync(
  Promise.resolve(Result.ok(10))
)
// => Ok(10)

await Result.err('fail').andAsync(
  Promise.resolve(Result.ok(42))
)
// => Err("fail")

Inherited from

ResultMethods.andAsync


andThenAsync()

andThenAsync<U, E2>(next): AsyncResult<U, TError | E2>

Defined in: src/types/methods.ts:857

Chains an asynchronous function that returns another Result.

If this result is Ok, next receives the success value. If this result is Err, the current error is preserved.

Type Parameters

Type ParameterDefault typeDescription
U-Next success value type.
E2neverNext error type.

Parameters

ParameterTypeDescription
next(value) => AsyncResult<U, E2>Async function that returns the next result.

Returns

AsyncResult<U, TError | E2>

See

andThen - Sync version

Example

ts
await Result.ok(userId).andThenAsync(async (id) => {
  const user = await fetchUser(id)
  return user ? Result.ok(user) : Result.err('not found')
})

await Result.err('fail').andThenAsync(
  async (x) => Result.ok(x * 2)
)
// => Err("fail")

Inherited from

ResultMethods.andThenAsync


orAsync()

orAsync<U, E2>(other): AsyncResult<TValue | U, E2>

Defined in: src/types/methods.ts:884

Returns this result when it is Ok, otherwise returns other.

Type Parameters

Type ParameterDefault typeDescription
UTValueAlternative success value type.
E2neverAlternative error type.

Parameters

ParameterTypeDescription
otherAsyncResult<U, E2>Async fallback result.

Returns

AsyncResult<TValue | U, E2>

See

or - Sync version

Example

ts
await Result.ok(5).orAsync(
  Promise.resolve(Result.ok(10))
)
// => Ok(5)

await Result.err('fail').orAsync(
  Promise.resolve(Result.ok(42))
)
// => Ok(42)

Inherited from

ResultMethods.orAsync


orElseAsync()

orElseAsync<U, E2>(fallback): AsyncResult<TValue | U, E2>

Defined in: src/types/methods.ts:911

Returns this result when it is Ok, otherwise invokes fallback asynchronously with the current error.

Useful for async recovery flows that depend on the failure reason.

Type Parameters

Type ParameterDefault typeDescription
UTValueRecovery success value type.
E2neverRecovery error type.

Parameters

ParameterTypeDescription
fallback(error) => AsyncResult<U, E2>Async function used to recover from an error.

Returns

AsyncResult<TValue | U, E2>

See

orElse - sync version

Example

ts
await Result.ok(5).orElseAsync(
  async () => Result.ok(0)
)
// => Ok(5)

await Result.err('not found').orElseAsync(
  async () => Result.ok(42)
)
// => Ok(42)

Inherited from

ResultMethods.orElseAsync

Async Transformation

mapAsync()

mapAsync<U>(mapper): AsyncResult<U, TError>

Defined in: src/types/methods.ts:720

Asynchronously transforms the success value.

If this result is Err, the original error is preserved.

Type Parameters

Type ParameterDescription
UMapped success value type.

Parameters

ParameterTypeDescription
mapper(value) => Promise<U>Async function applied to the success value.

Returns

AsyncResult<U, TError>

See

map - sync version

Example

ts
await Result.ok(1).mapAsync(async (x) => x + 1)
// => Ok(2)

await Result.err('fail').mapAsync(async (x) => x + 1)
// => Err('fail')

Inherited from

ResultMethods.mapAsync


mapErrAsync()

mapErrAsync<E2>(mapper): AsyncResult<TValue, E2>

Defined in: src/types/methods.ts:744

Asynchronously transforms the error value.

If this result is Ok, the original success value is preserved.

Type Parameters

Type ParameterDescription
E2Mapped error type.

Parameters

ParameterTypeDescription
mapper(error) => Promise<E2>Async function applied to the error value.

Returns

AsyncResult<TValue, E2>

See

mapErr - Sync version

Example

ts
await Result.ok(5).mapErrAsync(async (e) => e + 1)
// => Ok(5)

await Result.err('fail').mapErrAsync(
  async (e) => new Error(e)
)
// => Err(Error: "fail")

Inherited from

ResultMethods.mapErrAsync


mapOrAsync()

mapOrAsync<U>(mapper, defaultValue): Promise<U>

Defined in: src/types/methods.ts:765

Asynchronously maps the success value or returns the provided fallback when this result is Err.

Type Parameters

Type ParameterDescription
UReturn type.

Parameters

ParameterTypeDescription
mapper(value) => Promise<U>Async function applied to the success value.
defaultValueUValue returned when this result is Err.

Returns

Promise<U>

See

mapOr - Sync version

Example

ts
await Result.ok(5).mapOrAsync(async (x) => x * 2, 0)
// => 10

await Result.err('fail').mapOrAsync(async (x) => x * 2, 0)
// => 0

Inherited from

ResultMethods.mapOrAsync


mapOrElseAsync()

mapOrElseAsync<U>(okMapper, errMapper): Promise<U>

Defined in: src/types/methods.ts:794

Asynchronously maps either branch of the result into a shared output type.

Uses okMapper for Ok and errMapper for Err.

Type Parameters

Type ParameterDescription
UResult type

Parameters

ParameterTypeDescription
okMapper(value) => Promise<U>Async function applied to the success value.
errMapper(error) => Promise<U>Async function applied to the error value.

Returns

Promise<U>

See

mapOrElse - Sync version

Example

ts
await Result.ok(5).mapOrElseAsync(
  async (x) => x * 2,
  async () => -1
)
// => 10

await Result.err('fail').mapOrElseAsync(
  async (x) => x * 2,
  async () => -1
)
// => -1

Inherited from

ResultMethods.mapOrElseAsync

Combination

zip()

zip<U, E2>(other): Result<[TValue, U], TError | E2>

Defined in: src/types/methods.ts:538

Combines this result with another result into a tuple.

Returns Ok only when both results are successful. If either result is Err, the first encountered error is returned.

Type Parameters

Type ParameterDescription
USecond success type
E2Second error type

Parameters

ParameterTypeDescription
otherResult<U, E2>Result to combine with this one.

Returns

Result<[TValue, U], TError | E2>

See

zipWith - Combine and map in one step.

Example

ts
Result.ok(1).zip(Result.ok('a'))  // => Ok([1, 'a'])
Result.ok(1).zip(Result.err('b')) // => Err('b')

Inherited from

ResultMethods.zip


zipWith()

zipWith<U, R, E2>(other, combine): Result<R, TError | E2>

Defined in: src/types/methods.ts:573

Combines two successful results and maps their values.

Returns Ok only when both results are successful. If either result is Err, the first encountered error is returned.

Type Parameters

Type ParameterDescription
USecond success value type.
RCombined return type.
E2Second error type.

Parameters

ParameterTypeDescription
otherResult<U, E2>Second result.
combine(value, otherValue) => RFunction applied to both success values.

Returns

Result<R, TError | E2>

See

zip - Return a tuple instead of mapping.

Example

ts
Result.ok(2).zipWith(
  Result.ok(3),
  (a, b) => a + b
) // => Ok(5)

Result.ok('hello').zipWith(
  Result.ok('world'),
  (a, b) => `${a} ${b}`,
) // => Ok("hello world")

Result.ok(2).zipWith(
  Result.err('fail'),
  (a, b) => a + b,
) // => Err("fail")

Inherited from

ResultMethods.zipWith

Conversion

toJSON()

toJSON(): object

Defined in: src/types/index.ts:49

Converts this result into a plain JSON-serializable object.

Returns

object

NameTypeDefined in
errorTErrorsrc/types/index.ts:49
type"err"src/types/index.ts:49

Example

ts
Result.ok(42).toJSON()
// { type: 'ok', value: 42 }

Result.err('fail').toJSON()
// { type: 'err', error: 'fail' }

Overrides

ResultMethods.toJSON


toNullable()

toNullable(): TValue | null

Defined in: src/types/methods.ts:959

Converts this result into a nullable value.

Returns the success value when this result is Ok, otherwise returns null.

Returns

TValue | null

See

toValue - Return undefined on error instead.

Example

ts
Result.ok(42).toNullable()        // => 42
Result.err('failed').toNullable() // => null

Inherited from

ResultMethods.toNullable


toString()

toString(): string

Defined in: src/types/methods.ts:930

Returns a string representation of this result.

Formats the current value as Ok(value) or Err(error).

Returns

string

Example

ts
Result.ok(42).toString()      // => "Ok(42)"
Result.err('fail').toString() // => "Err("fail")"

Inherited from

ResultMethods.toString


toValue()

toValue(): TValue | undefined

Defined in: src/types/methods.ts:974

Converts this result into an optional value.

Returns the success value when this result is Ok, otherwise returns undefined.

Returns

TValue | undefined

See

toNullable - Return null on error instead.

Example

ts
Result.ok(42).toValue()        // => 42
Result.err('failed').toValue() // => undefined

Inherited from

ResultMethods.toValue

Extraction

expect()

expect(reason): TValue

Defined in: src/types/methods.ts:218

Returns the success value.

Parameters

ParameterTypeDescription
reasonstringCustom error message.

Returns

TValue

See

Throws

  • With reason and the original error as cause.

Examples

ts
Result.ok(42).expect('should exist')
// => 42
ts
Result.err('failed').expect('should exist')
// => throws Error("should exist", { cause: "failed" })

Inherited from

ResultMethods.expect


expectErr()

expectErr(reason): TError

Defined in: src/types/methods.ts:242

Returns the error value.

Throws with the provided message if this result is Ok.

Parameters

ParameterTypeDescription
reasonstringCustom error message.

Returns

TError

See

Throws

  • With reason and the original value as cause.

Examples

ts
Result.err('fail').expectErr('should be error')
// => 'fail'
ts
Result.ok(42).expectErr('should be error')
// => throws Error("should be error", { cause: 42 })

Inherited from

ResultMethods.expectErr


unwrap()

unwrap(): never

Defined in: src/types/index.ts:46

Returns the contained success value.

Throws if this result is Err.

Returns

never

See

Throws

  • If called on an Err result.

Examples

ts
Result.ok(42).unwrap() // => 42
ts
Result.err('fail').unwrap()
// => throws Error("Called unwrap on an Err value", { cause: "fail" })
ts
if (result.isOk()) {
  result.unwrap() // safe
}

Overrides

ResultMethods.unwrap


unwrapErr()

unwrapErr(): TError

Defined in: src/types/index.ts:47

Returns the contained error value.

Throws if this result is Ok.

Returns

TError

See

unwrap - Extract the success value.

Throws

  • If called on an Ok result.

Examples

ts
Result.err('failed').unwrapErr()
// => "failed"
ts
Result.ok(42).unwrapErr()
// => throws Error("Called unwrapErr on an Ok value", { cause: 42 })
ts
if (result.isErr()) {
  result.unwrapErr() // safe
}

Overrides

ResultMethods.unwrapErr


unwrapOr()

unwrapOr<TFallback>(defaultValue): TValue | TFallback

Defined in: src/types/methods.ts:179

Returns the success value, or the provided fallback when this result is Err.

Type Parameters

Type ParameterDefault typeDescription
TFallbackTValueType of the fallback value.

Parameters

ParameterTypeDescription
defaultValueTFallbackValue returned when this result is Err.

Returns

TValue | TFallback

See

Example

ts
Result.ok(42).unwrapOr(0)        // => 42
Result.err('failed').unwrapOr(0) // => 0

Inherited from

ResultMethods.unwrapOr


unwrapOrElse()

unwrapOrElse<TFallback>(fallback): TValue | TFallback

Defined in: src/types/methods.ts:196

Returns the success value, or computes a fallback from the error when this result is Err.

Type Parameters

Type ParameterDefault typeDescription
TFallbackTValueType returned by the fallback function.

Parameters

ParameterTypeDescription
fallback(error) => TFallbackFunction used to derive a fallback from the error.

Returns

TValue | TFallback

See

unwrapOr - Use a static fallback value.

Example

ts
Result.ok(42).unwrapOrElse((e) => e.length)        // => 42
Result.err('failed').unwrapOrElse((e) => e.length) // => 6

Inherited from

ResultMethods.unwrapOrElse

Inspection

contains()

Call Signature

contains<U>(value): boolean

Defined in: src/types/methods.ts:601

Returns true when this result is Ok and its value is strictly equal to value.

Uses === comparison.

Type Parameters
Type ParameterDescription
UComparable value type.
Parameters
ParameterTypeDescription
valueUExpected value.
Returns

boolean

Example
ts
Result.ok(42).contains(42)                 // => true
Result.ok({ id: 42 }).contains({ id: 42 }) // => true
Result.ok(42).contains(99)                 // => false
Result.err('fail').contains(42)            // => false
Inherited from

ResultMethods.contains

Call Signature

contains<U>(value, comparator): boolean

Defined in: src/types/methods.ts:625

Returns true when this result is Ok and the comparator determines both values are equivalent.

Useful for objects or custom equality rules.

Type Parameters
Type ParameterDescription
UExpected value type.
Parameters
ParameterTypeDescription
valueUExpected value.
comparator(actual, expected) => booleanCustom comparison function.
Returns

boolean

Example
ts
const user = { id: 1, name: 'John' }

Result.ok(user).contains(
  { id: 1 },
  (actual, expected) => actual.id === expected.id
) // => true
Inherited from

ResultMethods.contains


inspect()

inspect(action): this

Defined in: src/types/methods.ts:676

Runs a callback when this result is Ok.

Intended for side effects such as logging, metrics, or debugging. Returns the original result for chaining.

Parameters

ParameterTypeDescription
action(value) => voidCallback invoked with the success value.

Returns

this

See

Example

ts
Result.ok(42).inspect((value) => console.log(value))
// => logs 42, returns Ok(42)

Inherited from

ResultMethods.inspect


inspectErr()

inspectErr(action): this

Defined in: src/types/methods.ts:694

Runs a callback when this result is Err.

Intended for side effects such as logging, metrics, or debugging. Returns the original result for chaining.

Parameters

ParameterTypeDescription
action(error) => voidCallback invoked with the error value.

Returns

this

See

inspect - Side effects for success values.

Example

ts
Result.err('fail').inspectErr(e => console.log(e))
// => logs "fail", returns Err("fail")

Inherited from

ResultMethods.inspectErr


match()

match<L, R>(cases): L | R

Defined in: src/types/methods.ts:657

Pattern matches the current result.

Executes cases.ok for Ok or cases.err for Err, returning the selected branch result.

Type Parameters

Type ParameterDescription
LReturn type for the success branch.
RReturn type for the error branch.

Parameters

ParameterTypeDescription
casesMatchCases<TValue, TError, L, R>Branch handlers.

Returns

L | R

See

  • inspect - Run side effects for success values.
  • inspectErr - Run side effects for errors.

Examples

ts
Result.ok(5).match({
  ok: (x) => `Success: ${x * 2}`,
  err: (e) => `Error: ${e}`
})
// => "Success: 10"
ts
Result.err('not found').match({
  ok: (x) => `Value: ${x}`,
  err: (e) => `Error: ${e}`
})
// => "Error: not found"

Inherited from

ResultMethods.match

Transformation

filter()

filter(condition, reason?): Result<TValue, Error>

Defined in: src/types/methods.ts:355

Validates the success value with a predicate.

If this result is Ok and the predicate returns false, converts it to Err.

Existing Err values are preserved.

Parameters

ParameterTypeDescription
condition(value) => booleanPredicate used to validate the success value.
reason?stringOptional custom error message.

Returns

Result<TValue, Error>

See

Example

ts
Result.ok(42).filter((x) => x > 10)
// => Ok(42)

Result.ok(42).filter((x) => x > 50, 'Too small')
// => Err(Error: "Too small", { cause: 42 })

Inherited from

ResultMethods.filter


filterOrElse()

filterOrElse<E2>(condition, onFailure): Result<TValue, TError | E2>

Defined in: src/types/methods.ts:384

Validates the success value with a predicate.

If validation fails, converts the result to Err using the provided error factory.

Existing Err values are preserved.

Type Parameters

Type ParameterDescription
E2Additional error type.

Parameters

ParameterTypeDescription
condition(value) => booleanPredicate used to validate the success value.
onFailure(value) => E2Function used to create the failure value.

Returns

Result<TValue, TError | E2>

See

filter - Use a default error message.

Example

ts
Result.ok(42).filterOrElse(
  (x) => x > 10,
  (x) => new Error('Too small')
) // => Ok(42)

Result.ok(42).filterOrElse(
  (x) => x > 50,
  (x) => new Error('Too small')
) // => Err(Error: "Too small", { cause: 42 })

Inherited from

ResultMethods.filterOrElse


flatten()

flatten<U, E2>(this): Result<U, TError | E2>

Defined in: src/types/methods.ts:404

Flattens a nested Result.

Converts Result<Result<U, E2>, TError> into Result<U, TError | E2>.

Type Parameters

Type ParameterDescription
UInner success value type.
E2Inner error type.

Parameters

ParameterType
thisResult<Result<U, E2>, TError>

Returns

Result<U, TError | E2>

Example

ts
Result.ok(Result.ok(42)).flatten()      // => Ok(42)
Result.ok(Result.err('fail')).flatten() // => Err("fail")
Result.err('outer').flatten()           // Err("outer")

Inherited from

ResultMethods.flatten


map()

map<U>(mapper): Result<U, TError>

Defined in: src/types/methods.ts:267

Transforms the success value using the provided mapper.

If this result is Err, the original error is preserved.

Type Parameters

Type ParameterDescription
UMapped success value type.

Parameters

ParameterTypeDescription
mapper(value) => UFunction applied to the success value.

Returns

Result<U, TError>

See

  • mapAsync - Async version.
  • mapOr - Return a fallback value on error.
  • mapErr - Transform the error value instead.

Example

ts
Result.ok(42).map((x) => x * 2)      // => Ok(84)
Result.err('fail').map((x) => x * 2) // => Err("fail")

Inherited from

ResultMethods.map


mapErr()

mapErr<E2>(mapper): Result<TValue, E2>

Defined in: src/types/methods.ts:289

Transforms the error value using the provided mapper.

If this result is Ok, the original success value is preserved.

Type Parameters

Type ParameterDescription
E2Mapped error type.

Parameters

ParameterTypeDescription
mapper(error) => E2Function applied to the error value.

Returns

Result<TValue, E2>

See

mapErrAsync - Async version.

Example

ts
Result.err('not found').mapErr((e) => new Error(e))
// => Err(Error: "not found")

Result.ok(42).mapErr((e) => new Error(String(e)))
// => Ok(42)

Inherited from

ResultMethods.mapErr


mapOr()

mapOr<U>(mapper, defaultValue): U

Defined in: src/types/methods.ts:308

Maps the success value or returns the provided fallback when this result is Err.

Type Parameters

Type ParameterDescription
UReturn type.

Parameters

ParameterTypeDescription
mapper(value) => UFunction applied to the success value.
defaultValueUValue returned when this result is Err.

Returns

U

See

Example

ts
Result.ok(42).mapOr((x) => x * 2, 0)      // 84
Result.err('fail').mapOr((x) => x * 2, 0) // 0

Inherited from

ResultMethods.mapOr


mapOrElse()

mapOrElse<U>(okMapper, errMapper): U

Defined in: src/types/methods.ts:331

Maps either branch of the result into a shared output type.

Uses okMapper for Ok and errMapper for Err.

Type Parameters

Type ParameterDescription
UResult type.

Parameters

ParameterTypeDescription
okMapper(value) => UFunction applied to the success value.
errMapper(error) => UFunction applied to the error value.

Returns

U

See

mapOrElseAsync - Async version.

Example

ts
Result.ok(42).mapOrElse((x) => x * 2, () => -1)
// => 84

Result.err('fail').mapOrElse((x) => x * 2,() => -1)
// => -1

Inherited from

ResultMethods.mapOrElse