<A>(): Prism<Option.Option<A>, A>Prism that focuses on the value inside Option.Some.
When to use
Use when you have an Option<A> and want to read/update the inner value only
when it is Some.
Details
getResultfails with an error message when the option isNone.set(a)wrapsainOption.some(a).
Example (Accessing Some value)
import { Optic, Option, Result } from "effect"
const _some = Optic.id<Option.Option<number>>().compose(Optic.some())
console.log(Result.isSuccess(_some.getResult(Option.some(42))))
// Output: true
console.log(Result.isFailure(_some.getResult(Option.none())))
// Output: true
console.log(_some.set(10))
// Output: { _tag: "Some", value: 10 }export function function some<A>(): Prism<
Option.Option<A>,
A
>
Prism that focuses on the value inside Option.Some.
When to use
Use when you have an Option<A> and want to read/update the inner value only
when it is Some.
Details
getResult fails with an error message when the option is None.
set(a) wraps a in Option.some(a).
Example (Accessing Some value)
import { Optic, Option, Result } from "effect"
const _some = Optic.id<Option.Option<number>>().compose(Optic.some())
console.log(Result.isSuccess(_some.getResult(Option.some(42))))
// Output: true
console.log(Result.isFailure(_some.getResult(Option.none())))
// Output: true
console.log(_some.set(10))
// Output: { _tag: "Some", value: 10 }
some<function (type parameter) A in some<A>(): Prism<Option.Option<A>, A>A>(): 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 OptionOption.type Option<A> = Option.None<A> | Option.Some<A>The Option data type represents optional values. An Option<A> is either
Some<A>, containing a value of type A, or None, representing absence.
When to use
Use to represent initial values that may not yet exist
- Returning from partial functions (not defined for all inputs)
- Managing optional fields in data structures
Namespace containing utility types for Option.
When to use
Use to access type-level helpers associated with Option.
Option<function (type parameter) A in some<A>(): Prism<Option.Option<A>, A>A>, function (type parameter) A in some<A>(): Prism<Option.Option<A>, A>A> {
const const run: <A>(
e: Option.Option<A>
) => Result.Result<
Option.Some<A>,
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 OptionOption.const isSome: <A>(
self: Option<A>
) => self is Some<A>
Checks whether an Option contains a value (Some).
When to use
Use when you need to branch on a present Option before accessing .value.
Details
- Acts as a type guard, narrowing to
Some<A>
Example (Checking for Some)
import { Option } from "effect"
console.log(Option.isSome(Option.some(1)))
// Output: true
console.log(Option.isSome(Option.none()))
// Output: false
isSome, { 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 Some 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: Option.Option<A>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: Option.Option<A>
) => Result.Result<
Option.Some<A>,
SchemaIssue.Issue
>
run(s: Option.Option<A>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: Option.Some<A>) => AonSuccess: (s: Option.Some<A>(parameter) s: {
_tag: "Some";
_op: "Some";
value: A;
valueOrUndefined: 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: Option.Some<A>(parameter) s: {
_tag: "Some";
_op: "Some";
value: A;
valueOrUndefined: 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.Some<A>.value: Avalue
}),
import OptionOption.const some: <A>(value: A) => Option<A>Wraps the given value into an Option to represent its presence.
When to use
Use to wrap a known present value as Option
- Returning a successful result from a partial function
Details
- Always returns
Some<A>
- Does not filter
null or undefined; use
fromNullishOr
for that
Example (Wrapping a value)
import { Option } from "effect"
// ┌─── Option<number>
// ▼
const value = Option.some(1)
console.log(value)
// Output: { _id: 'Option', _tag: 'Some', value: 1 }
some
)
}