<A, E>(): Prism<Result.Result<A, E>, E>Prism that focuses on the failure value of a Result.
When to use
Use when you have a Result<A, E> and want to read/update E only when it
is a Failure.
Details
getResultfails when the result is aSuccess.set(e)producesResult.fail(e).
Example (Accessing failure)
import { Optic, Result } from "effect"
const _err = Optic.id<Result.Result<number, string>>().compose(Optic.failure())
console.log(Result.isSuccess(_err.getResult(Result.fail("oops"))))
// Output: true
console.log(Result.isFailure(_err.getResult(Result.succeed(42))))
// Output: trueexport function function failure<A, E>(): Prism<
Result.Result<A, E>,
E
>
Prism that focuses on the failure value of a Result.
When to use
Use when you have a Result<A, E> and want to read/update E only when it
is a Failure.
Details
getResult fails when the result is a Success.
set(e) produces Result.fail(e).
Example (Accessing failure)
import { Optic, Result } from "effect"
const _err = Optic.id<Result.Result<number, string>>().compose(Optic.failure())
console.log(Result.isSuccess(_err.getResult(Result.fail("oops"))))
// Output: true
console.log(Result.isFailure(_err.getResult(Result.succeed(42))))
// Output: true
failure<function (type parameter) A in failure<A, E>(): Prism<Result.Result<A, E>, E>A, function (type parameter) E in failure<A, E>(): Prism<Result.Result<A, E>, E>E>(): interface Prism<in out S, in out A>Focuses on a part A of S that may not be present (e.g. a union
variant or a validated subset).
When to use
Use when the focus is conditional — reading can fail (wrong variant, failed
validation).
- Building a new
S from A does not require the original S.
Details
getResult(s) returns Result.Success<A> when the focus matches, or
Result.Failure<string> with an error message.
set(a) always succeeds and returns a new S.
- Extends
Optional
.
- Composing two Prisms produces a Prism; composing a Prism with a
Example (Narrowing a tagged union)
import { Optic, Result } from "effect"
type Shape =
| { readonly _tag: "Circle"; readonly radius: number }
| { readonly _tag: "Rect"; readonly width: number }
const _circle = Optic.id<Shape>().tag("Circle")
console.log(Result.isSuccess(_circle.getResult({ _tag: "Circle", radius: 5 })))
// Output: true
console.log(Result.isFailure(_circle.getResult({ _tag: "Rect", width: 10 })))
// Output: true
Prism<import ResultResult.type Result<A, E = never> = Result.Success<A, E> | Result.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 failure<A, E>(): Prism<Result.Result<A, E>, E>A, function (type parameter) E in failure<A, E>(): Prism<Result.Result<A, E>, E>E>, function (type parameter) E in failure<A, E>(): Prism<Result.Result<A, E>, E>E> {
const const run: <A, E>(
e: Result.Result<A, E>
) => Result.Result<
Result.Failure<A, E>,
SchemaIssue.Issue
>
run = function runRefinement<T extends E, E>(
refinement: (e: E) => e is T,
annotations?: Schema.Annotations.Filter
): (e: E) => Result.Result<T, SchemaIssue.Issue>
runRefinement(import ResultResult.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, { Annotations.Augment.expected?: string | undefinedHuman-readable description of what a value is expected to satisfy.
Details
For filter and refinement failures, the default formatter uses
message first, then expected, and finally falls back to <filter>.
Use this to name a failed filter in the default message:
Expected <expected>, got <actual>.
expected: "a Result.Failure value" })
return function makePrism<S, A>(
getResult: (s: S) => Result.Result<A, string>,
set: (a: A) => S
): Prism<S, A>
Creates a
Prism
from a fallible getter and an infallible setter.
When to use
Use when reading can fail (the part may not exist in S), but building S
from A always succeeds.
Details
getResult should return Result.fail(message) on mismatch.
Example (Parsing a string to a number)
import { Optic, Result } from "effect"
const numeric = Optic.makePrism<string, number>(
(s) => {
const n = Number(s)
return Number.isNaN(n) ? Result.fail("not a number") : Result.succeed(n)
},
String
)
console.log(Result.isSuccess(numeric.getResult("42")))
// Output: true
console.log(numeric.set(42))
// Output: "42"
makePrism(
(s: Result.Result<A, E>s) =>
import ResultResult.const mapBoth: {
<E, E2, A, A2>(options: {
readonly onFailure: (left: E) => E2
readonly onSuccess: (right: A) => A2
}): (self: Result<A, E>) => Result<A2, E2>
<E, A, E2, A2>(
self: Result<A, E>,
options: {
readonly onFailure: (left: E) => E2
readonly onSuccess: (right: A) => A2
}
): Result<A2, E2>
}
mapBoth(const run: <A, E>(
e: Result.Result<A, E>
) => Result.Result<
Result.Failure<A, E>,
SchemaIssue.Issue
>
run(s: Result.Result<A, E>s), {
onFailure: (left: SchemaIssue.Issue) => stringonFailure: var String: StringConstructorAllows manipulation and formatting of text strings and determination and location of substrings within strings.
String,
onSuccess: (right: Result.Failure<A, E>) => EonSuccess: (s: Result.Failure<A, E>(parameter) s: {
_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;
}
s) => s: Result.Failure<A, E>(parameter) s: {
_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;
}
s.Failure<A, E>.failure: Efailure
}),
import ResultResult.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
)
}