<A, E>(): Prism<Result.Result<A, E>, A>Prism that focuses on the success value of a Result.
When to use
Use when you have a Result<A, E> and want to read/update A only when it
is a Success.
Details
getResultfails when the result is aFailure.set(a)producesResult.succeed(a).
Example (Accessing success)
import { Optic, Result } from "effect"
const _ok = Optic.id<Result.Result<number, string>>().compose(Optic.success())
console.log(Result.isSuccess(_ok.getResult(Result.succeed(42))))
// Output: true
console.log(Result.isFailure(_ok.getResult(Result.fail("err"))))
// Output: trueexport function function success<A, E>(): Prism<
Result.Result<A, E>,
A
>
Prism that focuses on the success value of a Result.
When to use
Use when you have a Result<A, E> and want to read/update A only when it
is a Success.
Details
getResult fails when the result is a Failure.
set(a) produces Result.succeed(a).
Example (Accessing success)
import { Optic, Result } from "effect"
const _ok = Optic.id<Result.Result<number, string>>().compose(Optic.success())
console.log(Result.isSuccess(_ok.getResult(Result.succeed(42))))
// Output: true
console.log(Result.isFailure(_ok.getResult(Result.fail("err"))))
// Output: true
success<function (type parameter) A in success<A, E>(): Prism<Result.Result<A, E>, A>A, function (type parameter) E in success<A, E>(): Prism<Result.Result<A, E>, A>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 success<A, E>(): Prism<Result.Result<A, E>, A>A, function (type parameter) E in success<A, E>(): Prism<Result.Result<A, E>, A>E>, function (type parameter) A in success<A, E>(): Prism<Result.Result<A, E>, A>A> {
const const run: <A, E>(
e: Result.Result<A, E>
) => Result.Result<
Result.Success<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 isSuccess: <A, E>(
self: Result<A, E>
) => self is Success<A, E>
Checks whether a Result is a Success.
When to use
Use to narrow a known Result to the Success variant.
Details
- Acts as a TypeScript type guard, narrowing to
Success<A, E>
- After narrowing, you can access
.success to read the value
Example (Narrowing to success)
import { Result } from "effect"
const result = Result.succeed(42)
if (Result.isSuccess(result)) {
console.log(result.success)
// Output: 42
}
isSuccess, { 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.Success 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.Success<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.Success<A, E>) => AonSuccess: (s: Result.Success<A, E>(parameter) s: {
_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;
}
s) => s: Result.Success<A, E>(parameter) s: {
_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;
}
s.Success<A, E>.success: Asuccess
}),
import ResultResult.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
)
}