<A, E>(self: Result<A, E>): AExtracts the success value or throws the raw failure value E.
When to use
Use when unchecked boundaries should turn failures into thrown exceptions.
Details
Success<A>returnsAFailure<E>throwsEdirectly- Use getOrThrowWith for a custom error object
Example (Unwrapping or throwing)
import { Result } from "effect"
console.log(Result.getOrThrow(Result.succeed(1)))
// Output: 1
// This would throw the string "error":
// Result.getOrThrow(Result.fail("error"))export const const getOrThrow: <A, E>(
self: Result<A, E>
) => A
Extracts the success value or throws the raw failure value E.
When to use
Use when unchecked boundaries should turn failures into thrown exceptions.
Details
Success<A> returns A
Failure<E> throws E directly
- Use
getOrThrowWith
for a custom error object
Example (Unwrapping or throwing)
import { Result } from "effect"
console.log(Result.getOrThrow(Result.succeed(1)))
// Output: 1
// This would throw the string "error":
// Result.getOrThrow(Result.fail("error"))
getOrThrow: <function (type parameter) A in <A, E>(self: Result<A, E>): AA, function (type parameter) E in <A, E>(self: Result<A, E>): AE>(self: Result<A, E>self: type Result<A, E = never> = Success<A, E> | Failure<A, E>A value that is either Success<A, E> or Failure<A, E>.
When to use
Use when both success and failure should remain available as data and
Option would lose failure information.
Details
- Use
succeed
/
fail
to construct
- Use
match
to fold both branches
- Use
isSuccess
/
isFailure
to narrow the type
E defaults to never, so Result<number> means a result that cannot fail.
Example (Creating and matching a Result)
import { Result } from "effect"
const success = Result.succeed(42)
const failure = Result.fail("something went wrong")
const message = Result.match(success, {
onSuccess: (value) => `Success: ${value}`,
onFailure: (error) => `Error: ${error}`
})
console.log(message)
// Output: "Success: 42"
Namespace containing type-level utilities for extracting the inner types
of a Result.
Example (Extracting inner types)
import type { Result } from "effect"
type R = Result.Result<number, string>
// number
type A = Result.Result.Success<R>
// string
type E = Result.Result.Failure<R>
Result<function (type parameter) A in <A, E>(self: Result<A, E>): AA, function (type parameter) E in <A, E>(self: Result<A, E>): AE>) => function (type parameter) A in <A, E>(self: Result<A, E>): AA = const getOrThrowWith: {
<E>(onFailure: (err: E) => unknown): <A>(
self: Result<A, E>
) => A
<A, E>(
self: Result<A, E>,
onFailure: (err: E) => unknown
): A
}
getOrThrowWith(import identityidentity)