<T>(): Transformation<Option.Option<T>, T | undefined>Decodes optional values into Option<T> and encodes Option.none() back to
an omitted optional value.
When to use
Use when you need a schema transformation to convert optional (possibly
undefined) values to Option.
Details
Decoding maps an absent or undefined value to Some(None) and a present
value to Some(Some(v)). Encoding maps Some(None) to None to omit the
value, and maps Some(Some(v)) to Some(v). This uses
transformOptional under the hood and filters out undefined on decode.
Example (Converting an optional value to an Option)
import { Schema, SchemaTransformation } from "effect"
const schema = Schema.Struct({
age: Schema.optional(Schema.Number).pipe(
Schema.decodeTo(
Schema.Option(Schema.Number),
SchemaTransformation.optionFromOptional()
)
)
})export function function optionFromOptional<
T
>(): Transformation<
Option.Option<T>,
T | undefined
>
Decodes optional values into Option<T> and encodes Option.none() back to
an omitted optional value.
When to use
Use when you need a schema transformation to convert optional (possibly
undefined) values to Option.
Details
Decoding maps an absent or undefined value to Some(None) and a present
value to Some(Some(v)). Encoding maps Some(None) to None to omit the
value, and maps Some(Some(v)) to Some(v). This uses
transformOptional under the hood and filters out undefined on decode.
Example (Converting an optional value to an Option)
import { Schema, SchemaTransformation } from "effect"
const schema = Schema.Struct({
age: Schema.optional(Schema.Number).pipe(
Schema.decodeTo(
Schema.Option(Schema.Number),
SchemaTransformation.optionFromOptional()
)
)
})
optionFromOptional<function (type parameter) T in optionFromOptional<T>(): Transformation<Option.Option<T>, T | undefined>T>(): class Transformation<in out T, in out E, RD = never, RE = never>class Transformation {
_tag: 'Transformation';
decode: SchemaGetter.Getter<T, E, RD>;
encode: SchemaGetter.Getter<E, T, RE>;
flip: () => Transformation<E, T, RE, RD>;
compose: <T2, RD2, RE2>(other: Transformation<T2, T, RD2, RE2>) => Transformation<T2, E, RD | RD2, RE | RE2>;
}
Represents a bidirectional transformation between a decoded type T and an encoded
type E, built from a pair of Getters.
When to use
Use when you need a schema transformation that defines how a schema converts
between two representations.
- You want to compose multiple transformations into a pipeline.
- You want to flip a transformation to swap decode/encode.
Details
This is the primary building block for Schema.decodeTo, Schema.encodeTo,
Schema.decode, Schema.encode, and Schema.link. Each direction is a
SchemaGetter.Getter that handles optionality, failure, and Effect services.
- Immutable —
flip() and compose() return new instances.
flip() swaps the decode and encode getters.
compose(other) chains: this.decode then other.decode for decoding,
other.encode then this.encode for encoding.
Example (Composing two transformations)
import { SchemaTransformation } from "effect"
const trimAndLower = SchemaTransformation.trim().compose(
SchemaTransformation.toLowerCase()
)
// decode: trim then lowercase
// encode: passthrough (both directions)
Transformation<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) T in optionFromOptional<T>(): Transformation<Option.Option<T>, T | undefined>T>, function (type parameter) T in optionFromOptional<T>(): Transformation<Option.Option<T>, T | undefined>T | undefined> {
return function transformOptional<
T,
E
>(options: {
readonly decode: (
input: Option.Option<E>
) => Option.Option<T>
readonly encode: (
input: Option.Option<T>
) => Option.Option<E>
}): Transformation<T, E>
Creates a Transformation where decode and encode operate on Option
values, giving full control over missing-key handling.
When to use
Use when you need a schema transformation to produce or consume Option.None
for absent keys.
- You are working with optional struct fields.
Details
- Each function receives
Option<input> and returns Option<output>.
Option.None input means the key is absent; returning Option.None
omits the key from the output.
- Pure and synchronous.
Example (Converting an optional key to Option)
import { Option, Schema, SchemaTransformation } from "effect"
const schema = Schema.Struct({
a: Schema.optionalKey(Schema.Number).pipe(
Schema.decodeTo(
Schema.Option(Schema.Number),
SchemaTransformation.transformOptional({
decode: Option.some,
encode: Option.flatten
})
)
)
})
transformOptional<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) T in optionFromOptional<T>(): Transformation<Option.Option<T>, T | undefined>T>, function (type parameter) T in optionFromOptional<T>(): Transformation<Option.Option<T>, T | undefined>T | undefined>({
decode: (
input: Option.Option<T | undefined>
) => Option.Option<Option.Option<T>>
decode: (ot: Option.Option<T | undefined>ot) => ot: Option.Option<T | undefined>ot.Pipeable.pipe<Option.Option<T | undefined>, Option.Option<Exclude<T, undefined>>, Option.Option<Option.Option<Exclude<T, undefined>>>>(this: Option.Option<T | undefined>, ab: (_: Option.Option<T | undefined>) => Option.Option<Exclude<T, undefined>>, bc: (_: Option.Option<Exclude<T, undefined>>) => Option.Option<Option.Option<Exclude<T, undefined>>>): Option.Option<Option.Option<Exclude<T, undefined>>> (+21 overloads)pipe(import OptionOption.const filter: {
<A, B extends A>(
refinement: Refinement<A, B>
): (self: Option<A>) => Option<B>
<A>(predicate: Predicate<A>): <B extends A>(
self: Option<B>
) => Option<B>
<A, B extends A>(
self: Option<A>,
refinement: Refinement<A, B>
): Option<B>
<A>(
self: Option<A>,
predicate: Predicate<A>
): Option<A>
}
filter(import PredicatePredicate.function isNotUndefined<A>(
input: A
): input is Exclude<A, undefined>
Checks whether a value is not undefined.
When to use
Use when you need a Predicate refinement that filters out undefined
while preserving other falsy values.
Details
Returns a refinement that excludes undefined.
Example (Filtering undefined values)
import { Predicate } from "effect"
const values = [1, undefined, 2]
const defined = values.filter(Predicate.isNotUndefined)
console.log(defined)
isNotUndefined), 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),
encode: <A>(
self: Option<Option<A>>
) => Option<A>
encode: import OptionOption.const flatten: <A>(
self: Option<Option<A>>
) => Option<A>
Flattens a nested Option<Option<A>> into Option<A>.
When to use
Use when you need to remove one layer of nested Option.
Details
Some(Some(value)) → Some(value)
Some(None) → None
None → None
Example (Flattening nested Options)
import { Option } from "effect"
console.log(Option.flatten(Option.some(Option.some("value"))))
// Output: { _id: 'Option', _tag: 'Some', value: 'value' }
console.log(Option.flatten(Option.some(Option.none())))
// Output: { _id: 'Option', _tag: 'None' }
flatten
})
}