Interface: Ok<TValue, TError>
Defined in: src/types/index.ts:18
Successful variant of Result.
See
Err - for the error variant
Example
const result = Result.ok(42)
result.isOk() // => true
result.unwrap() // => Ok(42)Extends
ResultMethods<TValue,TError>
Type Parameters
| Type Parameter | Default type | Description |
|---|---|---|
TValue | - | Type of the success value |
TError | never | Error type carried by Result. Present only for union compatibility |
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
Result.err('failed').isErr() // => true
Result.ok(42).isErr() // => falseInherited from
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
| Parameter | Type | Description |
|---|---|---|
condition | (error) => boolean | Predicate used to validate the error value. |
Returns
this is Err<TValue, TError>
See
isOkAnd - Opposite check for success values.
Example
Result.err('timeout').isErrAnd((e) => e === 'timeout') // => true
Result.ok(42).isErrAnd(() => true) // => falseInherited from
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
Result.ok(42).isOk() // => true
Result.err('failed').isOk() // => falseInherited from
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
| Parameter | Type | Description |
|---|---|---|
condition | (value) => boolean | Predicate used to validate the success value. |
Returns
this is Ok<TValue, TError>
See
isErrAnd - Opposite check for errors.
Example
Result.ok(42).isOkAnd((x) => x > 40) // => true
Result.ok(10).isOkAnd((x) => x > 40) // => false
Result.err('fail').isOkAnd(() => true) // => falseInherited from
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 Parameter | Default type | Description |
|---|---|---|
U | - | Next success value type. |
E2 | never | Next error type. |
Parameters
| Parameter | Type | Description |
|---|---|---|
other | Result<U, E2> | Result returned when this result is Ok. |
Returns
Result<U, TError | E2>
See
Example
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
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 Parameter | Default type | Description |
|---|---|---|
U | - | Next success value type. |
E2 | never | Next error type. |
Parameters
| Parameter | Type | Description |
|---|---|---|
next | (value) => Result<U, E2> | Function that returns the next result. |
Returns
Result<U, TError | E2>
See
- andThenAsync - Async version.
- map - Transform without flattening.
Example
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
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 Parameter | Default type | Description |
|---|---|---|
U | TValue | Alternative success type |
E2 | never | Alternative error type |
Parameters
| Parameter | Type | Description |
|---|---|---|
other | Result<U, E2> | Result used when this result is Err. |
Returns
Result<TValue | U, E2>
See
Example
Result.ok(1).or(Result.ok(2)) // => Ok(1)
Result.err('fail').or(Result.ok(42)) // => Ok(42)Inherited from
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 Parameter | Default type | Description |
|---|---|---|
U | TValue | Recovery success type |
E2 | never | Recovery error type |
Parameters
| Parameter | Type | Description |
|---|---|---|
fallback | (error) => Result<U, E2> | Function used to recover from an error. |
Returns
Result<TValue | U, E2>
See
- orElseAsync - Async version.
- or - Use a static fallback result.
Example
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
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 Parameter | Default type | Description |
|---|---|---|
U | - | Next success value type. |
E2 | never | Next error type. |
Parameters
| Parameter | Type | Description |
|---|---|---|
other | AsyncResult<U, E2> | Async result returned when this result is Ok. |
Returns
AsyncResult<U, TError | E2>
See
and - Sync version
Example
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
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 Parameter | Default type | Description |
|---|---|---|
U | - | Next success value type. |
E2 | never | Next error type. |
Parameters
| Parameter | Type | Description |
|---|---|---|
next | (value) => AsyncResult<U, E2> | Async function that returns the next result. |
Returns
AsyncResult<U, TError | E2>
See
andThen - Sync version
Example
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
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 Parameter | Default type | Description |
|---|---|---|
U | TValue | Alternative success value type. |
E2 | never | Alternative error type. |
Parameters
| Parameter | Type | Description |
|---|---|---|
other | AsyncResult<U, E2> | Async fallback result. |
Returns
AsyncResult<TValue | U, E2>
See
or - Sync version
Example
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
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 Parameter | Default type | Description |
|---|---|---|
U | TValue | Recovery success value type. |
E2 | never | Recovery error type. |
Parameters
| Parameter | Type | Description |
|---|---|---|
fallback | (error) => AsyncResult<U, E2> | Async function used to recover from an error. |
Returns
AsyncResult<TValue | U, E2>
See
orElse - sync version
Example
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
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 Parameter | Description |
|---|---|
U | Mapped success value type. |
Parameters
| Parameter | Type | Description |
|---|---|---|
mapper | (value) => Promise<U> | Async function applied to the success value. |
Returns
AsyncResult<U, TError>
See
map - sync version
Example
await Result.ok(1).mapAsync(async (x) => x + 1)
// => Ok(2)
await Result.err('fail').mapAsync(async (x) => x + 1)
// => Err('fail')Inherited from
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 Parameter | Description |
|---|---|
E2 | Mapped error type. |
Parameters
| Parameter | Type | Description |
|---|---|---|
mapper | (error) => Promise<E2> | Async function applied to the error value. |
Returns
AsyncResult<TValue, E2>
See
mapErr - Sync version
Example
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
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 Parameter | Description |
|---|---|
U | Return type. |
Parameters
| Parameter | Type | Description |
|---|---|---|
mapper | (value) => Promise<U> | Async function applied to the success value. |
defaultValue | U | Value returned when this result is Err. |
Returns
Promise<U>
See
mapOr - Sync version
Example
await Result.ok(5).mapOrAsync(async (x) => x * 2, 0)
// => 10
await Result.err('fail').mapOrAsync(async (x) => x * 2, 0)
// => 0Inherited from
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 Parameter | Description |
|---|---|
U | Result type |
Parameters
| Parameter | Type | Description |
|---|---|---|
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
await Result.ok(5).mapOrElseAsync(
async (x) => x * 2,
async () => -1
)
// => 10
await Result.err('fail').mapOrElseAsync(
async (x) => x * 2,
async () => -1
)
// => -1Inherited from
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 Parameter | Description |
|---|---|
U | Second success type |
E2 | Second error type |
Parameters
| Parameter | Type | Description |
|---|---|---|
other | Result<U, E2> | Result to combine with this one. |
Returns
Result<[TValue, U], TError | E2>
See
zipWith - Combine and map in one step.
Example
Result.ok(1).zip(Result.ok('a')) // => Ok([1, 'a'])
Result.ok(1).zip(Result.err('b')) // => Err('b')Inherited from
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 Parameter | Description |
|---|---|
U | Second success value type. |
R | Combined return type. |
E2 | Second error type. |
Parameters
| Parameter | Type | Description |
|---|---|---|
other | Result<U, E2> | Second result. |
combine | (value, otherValue) => R | Function applied to both success values. |
Returns
Result<R, TError | E2>
See
zip - Return a tuple instead of mapping.
Example
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
Conversion
toJSON()
toJSON():
object
Defined in: src/types/index.ts:25
Converts this result into a plain JSON-serializable object.
Returns
object
| Name | Type | Defined in |
|---|---|---|
type | "ok" | src/types/index.ts:25 |
value | TValue | src/types/index.ts:25 |
Example
Result.ok(42).toJSON()
// { type: 'ok', value: 42 }
Result.err('fail').toJSON()
// { type: 'err', error: 'fail' }Overrides
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
Result.ok(42).toNullable() // => 42
Result.err('failed').toNullable() // => nullInherited from
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
Result.ok(42).toString() // => "Ok(42)"
Result.err('fail').toString() // => "Err("fail")"Inherited from
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
Result.ok(42).toValue() // => 42
Result.err('failed').toValue() // => undefinedInherited from
Extraction
expect()
expect(
reason):TValue
Defined in: src/types/methods.ts:218
Returns the success value.
Parameters
| Parameter | Type | Description |
|---|---|---|
reason | string | Custom error message. |
Returns
TValue
See
Throws
- With
reasonand the original error ascause.
Examples
Result.ok(42).expect('should exist')
// => 42Result.err('failed').expect('should exist')
// => throws Error("should exist", { cause: "failed" })Inherited from
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
| Parameter | Type | Description |
|---|---|---|
reason | string | Custom error message. |
Returns
TError
See
Throws
- With
reasonand the original value ascause.
Examples
Result.err('fail').expectErr('should be error')
// => 'fail'Result.ok(42).expectErr('should be error')
// => throws Error("should be error", { cause: 42 })Inherited from
unwrap()
unwrap():
TValue
Defined in: src/types/index.ts:22
Returns the contained success value.
Throws if this result is Err.
Returns
TValue
See
Throws
- If called on an Err result.
Examples
Result.ok(42).unwrap() // => 42Result.err('fail').unwrap()
// => throws Error("Called unwrap on an Err value", { cause: "fail" })if (result.isOk()) {
result.unwrap() // safe
}Overrides
unwrapErr()
unwrapErr():
never
Defined in: src/types/index.ts:23
Returns the contained error value.
Throws if this result is Ok.
Returns
never
See
unwrap - Extract the success value.
Throws
- If called on an Ok result.
Examples
Result.err('failed').unwrapErr()
// => "failed"Result.ok(42).unwrapErr()
// => throws Error("Called unwrapErr on an Ok value", { cause: 42 })if (result.isErr()) {
result.unwrapErr() // safe
}Overrides
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 Parameter | Default type | Description |
|---|---|---|
TFallback | TValue | Type of the fallback value. |
Parameters
| Parameter | Type | Description |
|---|---|---|
defaultValue | TFallback | Value returned when this result is Err. |
Returns
TValue | TFallback
See
- unwrap - Throw on failure.
- unwrapOrElse - Compute a fallback lazily.
Example
Result.ok(42).unwrapOr(0) // => 42
Result.err('failed').unwrapOr(0) // => 0Inherited from
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 Parameter | Default type | Description |
|---|---|---|
TFallback | TValue | Type returned by the fallback function. |
Parameters
| Parameter | Type | Description |
|---|---|---|
fallback | (error) => TFallback | Function used to derive a fallback from the error. |
Returns
TValue | TFallback
See
unwrapOr - Use a static fallback value.
Example
Result.ok(42).unwrapOrElse((e) => e.length) // => 42
Result.err('failed').unwrapOrElse((e) => e.length) // => 6Inherited from
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 Parameter | Description |
|---|---|
U | Comparable value type. |
Parameters
| Parameter | Type | Description |
|---|---|---|
value | U | Expected value. |
Returns
boolean
Example
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) // => falseInherited from
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 Parameter | Description |
|---|---|
U | Expected value type. |
Parameters
| Parameter | Type | Description |
|---|---|---|
value | U | Expected value. |
comparator | (actual, expected) => boolean | Custom comparison function. |
Returns
boolean
Example
const user = { id: 1, name: 'John' }
Result.ok(user).contains(
{ id: 1 },
(actual, expected) => actual.id === expected.id
) // => trueInherited from
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
| Parameter | Type | Description |
|---|---|---|
action | (value) => void | Callback invoked with the success value. |
Returns
this
See
- inspectErr - Side effects for errors.
- match - Branch on both states.
Example
Result.ok(42).inspect((value) => console.log(value))
// => logs 42, returns Ok(42)Inherited from
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
| Parameter | Type | Description |
|---|---|---|
action | (error) => void | Callback invoked with the error value. |
Returns
this
See
inspect - Side effects for success values.
Example
Result.err('fail').inspectErr(e => console.log(e))
// => logs "fail", returns Err("fail")Inherited from
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 Parameter | Description |
|---|---|
L | Return type for the success branch. |
R | Return type for the error branch. |
Parameters
| Parameter | Type | Description |
|---|---|---|
cases | MatchCases<TValue, TError, L, R> | Branch handlers. |
Returns
L | R
See
- inspect - Run side effects for success values.
- inspectErr - Run side effects for errors.
Examples
Result.ok(5).match({
ok: (x) => `Success: ${x * 2}`,
err: (e) => `Error: ${e}`
})
// => "Success: 10"Result.err('not found').match({
ok: (x) => `Value: ${x}`,
err: (e) => `Error: ${e}`
})
// => "Error: not found"Inherited from
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
| Parameter | Type | Description |
|---|---|---|
condition | (value) => boolean | Predicate used to validate the success value. |
reason? | string | Optional custom error message. |
Returns
Result<TValue, Error>
See
- isOkAnd - Validate without transforming.
- filterOrElse - Provide a custom error value.
Example
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
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 Parameter | Description |
|---|---|
E2 | Additional error type. |
Parameters
| Parameter | Type | Description |
|---|---|---|
condition | (value) => boolean | Predicate used to validate the success value. |
onFailure | (value) => E2 | Function used to create the failure value. |
Returns
Result<TValue, TError | E2>
See
filter - Use a default error message.
Example
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
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 Parameter | Description |
|---|---|
U | Inner success value type. |
E2 | Inner error type. |
Parameters
| Parameter | Type |
|---|---|
this | Result<Result<U, E2>, TError> |
Returns
Result<U, TError | E2>
Example
Result.ok(Result.ok(42)).flatten() // => Ok(42)
Result.ok(Result.err('fail')).flatten() // => Err("fail")
Result.err('outer').flatten() // Err("outer")Inherited from
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 Parameter | Description |
|---|---|
U | Mapped success value type. |
Parameters
| Parameter | Type | Description |
|---|---|---|
mapper | (value) => U | Function 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
Result.ok(42).map((x) => x * 2) // => Ok(84)
Result.err('fail').map((x) => x * 2) // => Err("fail")Inherited from
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 Parameter | Description |
|---|---|
E2 | Mapped error type. |
Parameters
| Parameter | Type | Description |
|---|---|---|
mapper | (error) => E2 | Function applied to the error value. |
Returns
Result<TValue, E2>
See
mapErrAsync - Async version.
Example
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
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 Parameter | Description |
|---|---|
U | Return type. |
Parameters
| Parameter | Type | Description |
|---|---|---|
mapper | (value) => U | Function applied to the success value. |
defaultValue | U | Value returned when this result is Err. |
Returns
U
See
- mapOrAsync - Async version.
- mapOrElse - Compute the fallback lazily.
Example
Result.ok(42).mapOr((x) => x * 2, 0) // 84
Result.err('fail').mapOr((x) => x * 2, 0) // 0Inherited from
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 Parameter | Description |
|---|---|
U | Result type. |
Parameters
| Parameter | Type | Description |
|---|---|---|
okMapper | (value) => U | Function applied to the success value. |
errMapper | (error) => U | Function applied to the error value. |
Returns
U
See
mapOrElseAsync - Async version.
Example
Result.ok(42).mapOrElse((x) => x * 2, () => -1)
// => 84
Result.err('fail').mapOrElse((x) => x * 2,() => -1)
// => -1