Result<{}, never>Provides the starting point for the "do notation" simulation with Result.
When to use
Use to start a Result do-notation pipeline from an empty successful record
before adding named fields from Result-producing computations and pure
computed values.
Details
Creates a Result<{}> (success with an empty object). Use with
bind to add Result-producing fields and let
to add pure computed fields.
Example (Building an object step by step)
import { pipe, Result } from "effect"
const result = pipe(
Result.Do,
Result.bind("x", () => Result.succeed(2)),
Result.bind("y", () => Result.succeed(3)),
Result.let("sum", ({ x, y }) => x + y)
)
console.log(result)
// Output: { _tag: "Success", success: { x: 2, y: 3, sum: 5 }, ... }export const const Do: Result<{}>Provides the starting point for the "do notation" simulation with Result.
When to use
Use to start a Result do-notation pipeline from an empty successful record
before adding named fields from Result-producing computations and pure
computed values.
Details
Creates a Result<{}> (success with an empty object). Use with
bind
to add Result-producing fields and
let
to add pure computed fields.
Example (Building an object step by step)
import { pipe, Result } from "effect"
const result = pipe(
Result.Do,
Result.bind("x", () => Result.succeed(2)),
Result.bind("y", () => Result.succeed(3)),
Result.let("sum", ({ x, y }) => x + y)
)
console.log(result)
// Output: { _tag: "Success", success: { x: 2, y: 3, sum: 5 }, ... }
Do: 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<{}> = const succeed: <A>(right: A) => Result<A>Creates a Result holding a Success value.
Details
- Use when you have a value and want to lift it into the
Result type
- The error type
E defaults to never
Example (Wrapping a value)
import { Result } from "effect"
const result = Result.succeed(42)
console.log(Result.isSuccess(result))
// Output: true
succeed({})