<A, B extends A, E2>(
refinement: Refinement<NoInfer<A>, B>,
orFailWith: (value: NoInfer<A>) => E2
): <E>(self: Result<A, E>) => Result<B, E2 | E>
<A, E2>(
predicate: Predicate<NoInfer<A>>,
orFailWith: (value: NoInfer<A>) => E2
): <E>(self: Result<A, E>) => Result<A, E2 | E>
<A, E, B extends A, E2>(
self: Result<A, E>,
refinement: Refinement<A, B>,
orFailWith: (value: A) => E2
): Result<B, E | E2>
<A, E, E2>(
self: Result<A, E>,
predicate: Predicate<A>,
orFailWith: (value: A) => E2
): Result<A, E | E2>Validates the success value of a Result using a predicate, failing with a
custom error if the predicate returns false.
When to use
Use to validate an already-successful Result value with a predicate or
refinement.
Details
- If the result is already a
Failure, it is returned as-is - If the predicate passes, the
Successis returned unchanged - If the predicate fails,
orFailWithproduces the error for a newFailure - Also accepts a
Refinementto narrow the success type - The error type of the output is the union of both error types
Example (Filtering a success value)
import { pipe, Result } from "effect"
const result = pipe(
Result.succeed(0),
Result.filterOrFail(
(n) => n > 0,
(n) => `${n} is not positive`
)
)
console.log(result)
// Output: { _tag: "Failure", failure: "0 is not positive", ... }export const const filterOrFail: {
<A, B extends A, E2>(
refinement: Refinement<NoInfer<A>, B>,
orFailWith: (value: NoInfer<A>) => E2
): <E>(self: Result<A, E>) => Result<B, E2 | E>
<A, E2>(
predicate: Predicate<NoInfer<A>>,
orFailWith: (value: NoInfer<A>) => E2
): <E>(self: Result<A, E>) => Result<A, E2 | E>
<A, E, B extends A, E2>(
self: Result<A, E>,
refinement: Refinement<A, B>,
orFailWith: (value: A) => E2
): Result<B, E | E2>
<A, E, E2>(
self: Result<A, E>,
predicate: Predicate<A>,
orFailWith: (value: A) => E2
): Result<A, E | E2>
}
Validates the success value of a Result using a predicate, failing with a
custom error if the predicate returns false.
When to use
Use to validate an already-successful Result value with a predicate or
refinement.
Details
- If the result is already a
Failure, it is returned as-is
- If the predicate passes, the
Success is returned unchanged
- If the predicate fails,
orFailWith produces the error for a new Failure
- Also accepts a
Refinement to narrow the success type
- The error type of the output is the union of both error types
Example (Filtering a success value)
import { pipe, Result } from "effect"
const result = pipe(
Result.succeed(0),
Result.filterOrFail(
(n) => n > 0,
(n) => `${n} is not positive`
)
)
console.log(result)
// Output: { _tag: "Failure", failure: "0 is not positive", ... }
filterOrFail: {
<function (type parameter) A in <A, B extends A, E2>(refinement: Refinement<NoInfer<A>, B>, orFailWith: (value: NoInfer<A>) => E2): <E>(self: Result<A, E>) => Result<B, E2 | E>A, function (type parameter) B in <A, B extends A, E2>(refinement: Refinement<NoInfer<A>, B>, orFailWith: (value: NoInfer<A>) => E2): <E>(self: Result<A, E>) => Result<B, E2 | E>B extends function (type parameter) A in <A, B extends A, E2>(refinement: Refinement<NoInfer<A>, B>, orFailWith: (value: NoInfer<A>) => E2): <E>(self: Result<A, E>) => Result<B, E2 | E>A, function (type parameter) E2 in <A, B extends A, E2>(refinement: Refinement<NoInfer<A>, B>, orFailWith: (value: NoInfer<A>) => E2): <E>(self: Result<A, E>) => Result<B, E2 | E>E2>(
refinement: Refinement<NoInfer<A>, B>refinement: import RefinementRefinement<type NoInfer<A> = [A][A extends any ? 0 : never]Prevents TypeScript from inferring a type parameter from a specific
position.
When to use
Use when a function parameter must match an inferred type without becoming
an inference source.
Details
The parameter using NoInfer must still match the inferred type.
Example (Controlling inference)
import type { Types } from "effect"
declare function withDefault<T>(value: T, fallback: Types.NoInfer<T>): T
// T is inferred as "a" | "b" from the first argument only
const result = withDefault<"a" | "b">("a", "b")
NoInfer<function (type parameter) A in <A, B extends A, E2>(refinement: Refinement<NoInfer<A>, B>, orFailWith: (value: NoInfer<A>) => E2): <E>(self: Result<A, E>) => Result<B, E2 | E>A>, function (type parameter) B in <A, B extends A, E2>(refinement: Refinement<NoInfer<A>, B>, orFailWith: (value: NoInfer<A>) => E2): <E>(self: Result<A, E>) => Result<B, E2 | E>B>,
orFailWith: (value: NoInfer<A>) => E2orFailWith: (value: NoInfer<A>value: type NoInfer<A> = [A][A extends any ? 0 : never]Prevents TypeScript from inferring a type parameter from a specific
position.
When to use
Use when a function parameter must match an inferred type without becoming
an inference source.
Details
The parameter using NoInfer must still match the inferred type.
Example (Controlling inference)
import type { Types } from "effect"
declare function withDefault<T>(value: T, fallback: Types.NoInfer<T>): T
// T is inferred as "a" | "b" from the first argument only
const result = withDefault<"a" | "b">("a", "b")
NoInfer<function (type parameter) A in <A, B extends A, E2>(refinement: Refinement<NoInfer<A>, B>, orFailWith: (value: NoInfer<A>) => E2): <E>(self: Result<A, E>) => Result<B, E2 | E>A>) => function (type parameter) E2 in <A, B extends A, E2>(refinement: Refinement<NoInfer<A>, B>, orFailWith: (value: NoInfer<A>) => E2): <E>(self: Result<A, E>) => Result<B, E2 | E>E2
): <function (type parameter) E in <E>(self: Result<A, E>): Result<B, E2 | E>E>(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, B extends A, E2>(refinement: Refinement<NoInfer<A>, B>, orFailWith: (value: NoInfer<A>) => E2): <E>(self: Result<A, E>) => Result<B, E2 | E>A, function (type parameter) E in <E>(self: Result<A, E>): Result<B, E2 | E>E>) => 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) B in <A, B extends A, E2>(refinement: Refinement<NoInfer<A>, B>, orFailWith: (value: NoInfer<A>) => E2): <E>(self: Result<A, E>) => Result<B, E2 | E>B, function (type parameter) E2 in <A, B extends A, E2>(refinement: Refinement<NoInfer<A>, B>, orFailWith: (value: NoInfer<A>) => E2): <E>(self: Result<A, E>) => Result<B, E2 | E>E2 | function (type parameter) E in <E>(self: Result<A, E>): Result<B, E2 | E>E>
<function (type parameter) A in <A, E2>(predicate: Predicate<NoInfer<A>>, orFailWith: (value: NoInfer<A>) => E2): <E>(self: Result<A, E>) => Result<A, E2 | E>A, function (type parameter) E2 in <A, E2>(predicate: Predicate<NoInfer<A>>, orFailWith: (value: NoInfer<A>) => E2): <E>(self: Result<A, E>) => Result<A, E2 | E>E2>(
predicate: Predicate<NoInfer<A>>predicate: import PredicatePredicate<type NoInfer<A> = [A][A extends any ? 0 : never]Prevents TypeScript from inferring a type parameter from a specific
position.
When to use
Use when a function parameter must match an inferred type without becoming
an inference source.
Details
The parameter using NoInfer must still match the inferred type.
Example (Controlling inference)
import type { Types } from "effect"
declare function withDefault<T>(value: T, fallback: Types.NoInfer<T>): T
// T is inferred as "a" | "b" from the first argument only
const result = withDefault<"a" | "b">("a", "b")
NoInfer<function (type parameter) A in <A, E2>(predicate: Predicate<NoInfer<A>>, orFailWith: (value: NoInfer<A>) => E2): <E>(self: Result<A, E>) => Result<A, E2 | E>A>>,
orFailWith: (value: NoInfer<A>) => E2orFailWith: (value: NoInfer<A>value: type NoInfer<A> = [A][A extends any ? 0 : never]Prevents TypeScript from inferring a type parameter from a specific
position.
When to use
Use when a function parameter must match an inferred type without becoming
an inference source.
Details
The parameter using NoInfer must still match the inferred type.
Example (Controlling inference)
import type { Types } from "effect"
declare function withDefault<T>(value: T, fallback: Types.NoInfer<T>): T
// T is inferred as "a" | "b" from the first argument only
const result = withDefault<"a" | "b">("a", "b")
NoInfer<function (type parameter) A in <A, E2>(predicate: Predicate<NoInfer<A>>, orFailWith: (value: NoInfer<A>) => E2): <E>(self: Result<A, E>) => Result<A, E2 | E>A>) => function (type parameter) E2 in <A, E2>(predicate: Predicate<NoInfer<A>>, orFailWith: (value: NoInfer<A>) => E2): <E>(self: Result<A, E>) => Result<A, E2 | E>E2
): <function (type parameter) E in <E>(self: Result<A, E>): Result<A, E2 | E>E>(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, E2>(predicate: Predicate<NoInfer<A>>, orFailWith: (value: NoInfer<A>) => E2): <E>(self: Result<A, E>) => Result<A, E2 | E>A, function (type parameter) E in <E>(self: Result<A, E>): Result<A, E2 | E>E>) => 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, E2>(predicate: Predicate<NoInfer<A>>, orFailWith: (value: NoInfer<A>) => E2): <E>(self: Result<A, E>) => Result<A, E2 | E>A, function (type parameter) E2 in <A, E2>(predicate: Predicate<NoInfer<A>>, orFailWith: (value: NoInfer<A>) => E2): <E>(self: Result<A, E>) => Result<A, E2 | E>E2 | function (type parameter) E in <E>(self: Result<A, E>): Result<A, E2 | E>E>
<function (type parameter) A in <A, E, B extends A, E2>(self: Result<A, E>, refinement: Refinement<A, B>, orFailWith: (value: A) => E2): Result<B, E | E2>A, function (type parameter) E in <A, E, B extends A, E2>(self: Result<A, E>, refinement: Refinement<A, B>, orFailWith: (value: A) => E2): Result<B, E | E2>E, function (type parameter) B in <A, E, B extends A, E2>(self: Result<A, E>, refinement: Refinement<A, B>, orFailWith: (value: A) => E2): Result<B, E | E2>B extends function (type parameter) A in <A, E, B extends A, E2>(self: Result<A, E>, refinement: Refinement<A, B>, orFailWith: (value: A) => E2): Result<B, E | E2>A, function (type parameter) E2 in <A, E, B extends A, E2>(self: Result<A, E>, refinement: Refinement<A, B>, orFailWith: (value: A) => E2): Result<B, E | E2>E2>(
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, B extends A, E2>(self: Result<A, E>, refinement: Refinement<A, B>, orFailWith: (value: A) => E2): Result<B, E | E2>A, function (type parameter) E in <A, E, B extends A, E2>(self: Result<A, E>, refinement: Refinement<A, B>, orFailWith: (value: A) => E2): Result<B, E | E2>E>,
refinement: Refinement<A, B>refinement: import RefinementRefinement<function (type parameter) A in <A, E, B extends A, E2>(self: Result<A, E>, refinement: Refinement<A, B>, orFailWith: (value: A) => E2): Result<B, E | E2>A, function (type parameter) B in <A, E, B extends A, E2>(self: Result<A, E>, refinement: Refinement<A, B>, orFailWith: (value: A) => E2): Result<B, E | E2>B>,
orFailWith: (value: A) => E2orFailWith: (value: Avalue: function (type parameter) A in <A, E, B extends A, E2>(self: Result<A, E>, refinement: Refinement<A, B>, orFailWith: (value: A) => E2): Result<B, E | E2>A) => function (type parameter) E2 in <A, E, B extends A, E2>(self: Result<A, E>, refinement: Refinement<A, B>, orFailWith: (value: A) => E2): Result<B, E | E2>E2
): 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) B in <A, E, B extends A, E2>(self: Result<A, E>, refinement: Refinement<A, B>, orFailWith: (value: A) => E2): Result<B, E | E2>B, function (type parameter) E in <A, E, B extends A, E2>(self: Result<A, E>, refinement: Refinement<A, B>, orFailWith: (value: A) => E2): Result<B, E | E2>E | function (type parameter) E2 in <A, E, B extends A, E2>(self: Result<A, E>, refinement: Refinement<A, B>, orFailWith: (value: A) => E2): Result<B, E | E2>E2>
<function (type parameter) A in <A, E, E2>(self: Result<A, E>, predicate: Predicate<A>, orFailWith: (value: A) => E2): Result<A, E | E2>A, function (type parameter) E in <A, E, E2>(self: Result<A, E>, predicate: Predicate<A>, orFailWith: (value: A) => E2): Result<A, E | E2>E, function (type parameter) E2 in <A, E, E2>(self: Result<A, E>, predicate: Predicate<A>, orFailWith: (value: A) => E2): Result<A, E | E2>E2>(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, E2>(self: Result<A, E>, predicate: Predicate<A>, orFailWith: (value: A) => E2): Result<A, E | E2>A, function (type parameter) E in <A, E, E2>(self: Result<A, E>, predicate: Predicate<A>, orFailWith: (value: A) => E2): Result<A, E | E2>E>, predicate: Predicate<A>predicate: import PredicatePredicate<function (type parameter) A in <A, E, E2>(self: Result<A, E>, predicate: Predicate<A>, orFailWith: (value: A) => E2): Result<A, E | E2>A>, orFailWith: (value: A) => E2orFailWith: (value: Avalue: function (type parameter) A in <A, E, E2>(self: Result<A, E>, predicate: Predicate<A>, orFailWith: (value: A) => E2): Result<A, E | E2>A) => function (type parameter) E2 in <A, E, E2>(self: Result<A, E>, predicate: Predicate<A>, orFailWith: (value: A) => E2): Result<A, E | E2>E2): 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, E2>(self: Result<A, E>, predicate: Predicate<A>, orFailWith: (value: A) => E2): Result<A, E | E2>A, function (type parameter) E in <A, E, E2>(self: Result<A, E>, predicate: Predicate<A>, orFailWith: (value: A) => E2): Result<A, E | E2>E | function (type parameter) E2 in <A, E, E2>(self: Result<A, E>, predicate: Predicate<A>, orFailWith: (value: A) => E2): Result<A, E | E2>E2>
} = import dualdual(3, <function (type parameter) A in <A, E, E2>(self: Result<A, E>, predicate: Predicate<A>, orFailWith: (value: A) => E2): Result<A, E | E2>A, function (type parameter) E in <A, E, E2>(self: Result<A, E>, predicate: Predicate<A>, orFailWith: (value: A) => E2): Result<A, E | E2>E, function (type parameter) E2 in <A, E, E2>(self: Result<A, E>, predicate: Predicate<A>, orFailWith: (value: A) => E2): Result<A, E | E2>E2>(
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, E2>(self: Result<A, E>, predicate: Predicate<A>, orFailWith: (value: A) => E2): Result<A, E | E2>A, function (type parameter) E in <A, E, E2>(self: Result<A, E>, predicate: Predicate<A>, orFailWith: (value: A) => E2): Result<A, E | E2>E>,
predicate: Predicate<A>predicate: import PredicatePredicate<function (type parameter) A in <A, E, E2>(self: Result<A, E>, predicate: Predicate<A>, orFailWith: (value: A) => E2): Result<A, E | E2>A>,
orFailWith: (value: A) => E2orFailWith: (value: Avalue: function (type parameter) A in <A, E, E2>(self: Result<A, E>, predicate: Predicate<A>, orFailWith: (value: A) => E2): Result<A, E | E2>A) => function (type parameter) E2 in <A, E, E2>(self: Result<A, E>, predicate: Predicate<A>, orFailWith: (value: A) => E2): Result<A, E | E2>E2
): 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, E2>(self: Result<A, E>, predicate: Predicate<A>, orFailWith: (value: A) => E2): Result<A, E | E2>A, function (type parameter) E in <A, E, E2>(self: Result<A, E>, predicate: Predicate<A>, orFailWith: (value: A) => E2): Result<A, E | E2>E | function (type parameter) E2 in <A, E, E2>(self: Result<A, E>, predicate: Predicate<A>, orFailWith: (value: A) => E2): Result<A, E | E2>E2> => const flatMap: {
<A, A2, E2>(f: (a: A) => Result<A2, E2>): <E>(
self: Result<A, E>
) => Result<A2, E | E2>
<A, E, A2, E2>(
self: Result<A, E>,
f: (a: A) => Result<A2, E2>
): Result<A2, E | E2>
}
flatMap(self: Result<A, E>self, (a: Aa) => predicate: Predicate<A>predicate(a: Aa) ? 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(a: Aa) : const fail: <E>(
left: E
) => Result<never, E>
Creates a Result holding a Failure value.
When to use
Use to represent a failed Result with a typed failure value.
Details
- The success type
A defaults to never
Example (Creating a failure)
import { Result } from "effect"
const result = Result.fail("Something went wrong")
console.log(Result.isFailure(result))
// Output: true
fail(orFailWith: (value: A) => E2orFailWith(a: Aa))))