Package: effect
Module: Optic
Focuses on exactly one part A inside a whole S.
When to use
Use when you always have a value to read and need the original S to produce
the updated whole, unlike Iso.
Details
get(s) always succeeds and returns A.replace(a, s) returns a new S with the focused part replaced.Optional.Prism or Optional produces an
Optional.Example (Focusing on a struct field)
import { Optic } from "effect"
type Person = { readonly name: string; readonly age: number }
const _name = Optic.id<Person>().key("name")
console.log(_name.get({ name: "Alice", age: 30 }))
// Output: "Alice"
See
makeLens — constructorIso — when conversion is lossless in both directionsOptional — when reading can also failSignature
export interface Lens<in out S, in out A> extends Optional<S, A> {
readonly get: (s: S) => A
}
Since v4.0.0