Traversal<S, A>An optic that focuses on zero or more elements of type A inside S.
When to use
Use when you want to read/update multiple elements at once (e.g. all items in an array, or a filtered subset).
Details
- Technically
Optional<S, ReadonlyArray<A>>— the focused value is an array of all matched elements. - Use
.forEach()to add per-element sub-optics (filtering, drilling deeper). - Use
.modifyAll(f)to map a function over every focused element. - Use getAll to extract all focused elements as a plain array.
Example (Traversing array elements with a filter)
import { Optic, Schema } from "effect"
type S = { readonly items: ReadonlyArray<number> }
const _positive = Optic.id<S>()
.key("items")
.forEach((n) => n.check(Schema.isGreaterThan(0)))
const getPositive = Optic.getAll(_positive)
console.log(getPositive({ items: [1, -2, 3] }))
// Output: [1, 3]export interface interface Traversal<in out S, in out A>An optic that focuses on zero or more elements of type A inside S.
When to use
Use when you want to read/update multiple elements at once (e.g. all items in
an array, or a filtered subset).
Details
- Technically
Optional<S, ReadonlyArray<A>> — the focused value is an
array of all matched elements.
- Use
.forEach() to add per-element sub-optics (filtering, drilling
deeper).
- Use
.modifyAll(f) to map a function over every focused element.
- Use
getAll
to extract all focused elements as a plain array.
Example (Traversing array elements with a filter)
import { Optic, Schema } from "effect"
type S = { readonly items: ReadonlyArray<number> }
const _positive = Optic.id<S>()
.key("items")
.forEach((n) => n.check(Schema.isGreaterThan(0)))
const getPositive = Optic.getAll(_positive)
console.log(getPositive({ items: [1, -2, 3] }))
// Output: [1, 3]
Traversal<in out function (type parameter) S in Traversal<in out S, in out A>S, in out function (type parameter) A in Traversal<in out S, in out A>A> extends interface Optional<in out S, in out A>The most general optic — both reading and writing can fail.
When to use
Use when the focus may not exist in S and writing a new A back may also
fail, for example when the source no longer matches the expected shape. This
is the base type extended by
Iso
,
Lens
,
Prism
, and
Details
getResult(s) returns Result.Success<A> or Result.Failure<string>.
replaceResult(a, s) returns Result.Success<S> or
Result.Failure<string>.
replace(a, s) returns the original s on failure (never throws).
modify(f) returns the original s on failure (never throws).
- All operations are pure; inputs are never mutated.
Example (Focusing on an optional record key)
import { Optic, Result } from "effect"
type Env = { [key: string]: string }
const _home = Optic.id<Env>().at("HOME")
console.log(Result.isSuccess(_home.getResult({ HOME: "/root" })))
// Output: true
console.log(Result.isFailure(_home.getResult({ PATH: "/bin" })))
// Output: true
// replace returns original on failure
console.log(_home.replace("/new", { PATH: "/bin" }))
// Output: { PATH: "/bin" }
Optional<function (type parameter) S in Traversal<in out S, in out A>S, interface ReadonlyArray<T>ReadonlyArray<function (type parameter) A in Traversal<in out S, in out A>A>> {}