MCPcopy Create free account
hub / github.com/Effect-TS/effect / Optional

Interface Optional

packages/effect/src/Optic.ts:445–874  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

443 * @since 4.0.0
444 */
445export interface Optional<in out S, in out A> {
446 /**
447 * Attempts to read the focus `A` from the whole `S`. Returns
448 * `Result.Success<A>` when the focus exists, or
449 * `Result.Failure<SchemaIssue.Issue>` with a structured issue otherwise.
450 */
451 readonly getResult: (s: S) => Result.Result<A, SchemaIssue.Issue>
452 /**
453 * Replaces the focus in `S` with a new `A`. Returns the original `s`
454 * unchanged when the optic cannot focus (never throws).
455 */
456 readonly replace: (a: A, s: S) => S
457 /**
458 * Like {@link replace}, but returns an explicit `Result` so callers can
459 * detect and handle failure.
460 */
461 readonly replaceResult: (a: A, s: S) => Result.Result<S, SchemaIssue.Issue>
462 /**
463 * Composes this optic with another. The result type is the weakest of
464 * the two: Iso + Iso = Iso, Lens + Prism = Optional, etc.
465 *
466 * **Example** (Composing a lens with a prism)
467 *
468 * ```ts import.meta.vitest
469 * import { Optic, Option, Result } from "effect"
470 *
471 * type State = { value: Option.Option<number> }
472 *
473 * const _inner = Optic.id<State>().key("value").compose(Optic.some())
474 * // _inner is Optional<State, number>
475 * _inner.getResult({ value: Option.some(1) }) // => Result.succeed(1)
476 * ```
477 *
478 * @see {@link id} — start a composition chain
479 */
480 compose<B>(this: Iso<S, A>, that: Iso<A, B>): Iso<S, B>
481 compose<B>(this: Lens<S, A>, that: Lens<A, B>): Lens<S, B>
482 compose<B>(this: Prism<S, A>, that: Prism<A, B>): Prism<S, B>
483 compose<B>(this: Optional<S, A>, that: Optional<A, B>): Optional<S, B>
484
485 /**
486 * Returns a function `(s: S) => S` that applies `f` to the focused value.
487 * If the optic cannot focus, the original `s` is returned unchanged.
488 *
489 * **Example** (Incrementing a nested field)
490 *
491 * ```ts import.meta.vitest
492 * import { Optic } from "effect"
493 *
494 * type S = { readonly a: { readonly b: number } }
495 * const _b = Optic.id<S>().key("a").key("b")
496 *
497 * const inc = _b.modify((n) => n + 1)
498 * inc({ a: { b: 1 } }) // => { a: { b: 2 } }
499 * ```
500 */
501 modify(f: (a: A) => A): (s: S) => S
502

Callers 84

Graph.tst.tsFile · 0.65
Predicate.tst.tsFile · 0.65
Optic.tst.tsFile · 0.65
Optic.test.tsFile · 0.65
Graph.test.tsFile · 0.65
Predicate.test.tsFile · 0.65
Function.test.tsFile · 0.65
Schema.test.tsFile · 0.65
Array.tst.tsFile · 0.65
Record.test.tsFile · 0.65
Chunk.test.tsFile · 0.65

Implementers 1

OptionalImplpackages/effect/src/Optic.ts

Calls

no outgoing calls

Tested by

no test coverage detected