Package: effect
Module: Optic
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).
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.Optional.Lens produces an Optional.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
See
makePrism — constructorfromChecks — build a Prism from schema checksLens — when reading always succeedsSignature
export interface Prism<in out S, in out A> extends Optional<S, A> {
readonly set: (a: A) => S
}
Since v4.0.0