<A, E>(self: Result<A, E>): Result<E, A>Swaps the success and failure channels of a Result.
When to use
Use to swap channels when failure-focused operations are easier through success-oriented combinators.
Details
Success<A>becomesFailure<A>(i.e.,Result<E, A>)Failure<E>becomesSuccess<E>(i.e.,Result<E, A>)- Useful when you want to apply success-oriented operations (like
map) to the error channel, then flip back
Example (Swapping channels)
import { Result } from "effect"
console.log(Result.flip(Result.succeed(42)))
// Output: { _tag: "Failure", failure: 42, ... }
console.log(Result.flip(Result.fail("error")))
// Output: { _tag: "Success", success: "error", ... }export const const flip: <A, E>(
self: Result<A, E>
) => Result<E, A>
Swaps the success and failure channels of a Result.
When to use
Use to swap channels when failure-focused operations are easier through
success-oriented combinators.
Details
Success<A> becomes Failure<A> (i.e., Result<E, A>)
Failure<E> becomes Success<E> (i.e., Result<E, A>)
- Useful when you want to apply success-oriented operations (like
map)
to the error channel, then flip back
Example (Swapping channels)
import { Result } from "effect"
console.log(Result.flip(Result.succeed(42)))
// Output: { _tag: "Failure", failure: 42, ... }
console.log(Result.flip(Result.fail("error")))
// Output: { _tag: "Success", success: "error", ... }
flip = <function (type parameter) A in <A, E>(self: Result<A, E>): Result<E, A>A, function (type parameter) E in <A, E>(self: Result<A, E>): Result<E, A>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, E>(self: Result<A, E>): Result<E, A>A, function (type parameter) E in <A, E>(self: Result<A, E>): Result<E, A>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) E in <A, E>(self: Result<A, E>): Result<E, A>E, function (type parameter) A in <A, E>(self: Result<A, E>): Result<E, A>A> =>
const isFailure: <A, E>(
self: Result<A, E>
) => self is Failure<A, E>
Checks whether a Result is a Failure.
When to use
Use to narrow a known Result to the Failure variant.
Details
- Acts as a TypeScript type guard, narrowing to
Failure<A, E>
- After narrowing, you can access
.failure to read the error value
Example (Narrowing to failure)
import { Result } from "effect"
const result = Result.fail("oops")
if (Result.isFailure(result)) {
console.log(result.failure)
// Output: "oops"
}
isFailure(self: Result<A, E>self) ? 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(self: Result<A, E>(parameter) self: {
_tag: "Failure";
_op: "Failure";
failure: E;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
self.Failure<A, E>.failure: Efailure) : 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(self: Result<A, E>(parameter) self: {
_tag: "Success";
_op: "Success";
success: A;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
self.Success<A, E>.success: Asuccess)