OptionFromOptionalNullOr<S>Decodes an optional or null or undefined value A to a required Option<A>
value.
Details
Decoding maps a missing key, undefined, or null to None, and maps all
other values to Some. Encoding maps Some to its value. None is encoded
according to options.onNoneEncoding: "omit" encodes a missing key,
null encodes null, and undefined encodes undefined.
export interface interface OptionFromOptionalNullOr<S extends Constraint>Decodes an optional or null or undefined value A to a required Option<A>
value.
Details
Decoding maps a missing key, undefined, or null to None, and maps all
other values to Some. Encoding maps Some to its value. None is encoded
according to options.onNoneEncoding: "omit" encodes a missing key,
null encodes null, and undefined encodes undefined.
Type-level representation returned by
OptionFromOptionalNullOr
.
OptionFromOptionalNullOr<function (type parameter) S in OptionFromOptionalNullOr<S extends Constraint>S extends Constraint>
extends interface decodeTo<To extends Constraint, From extends Constraint, RD = never, RE = never>Creates a schema that transforms from a source schema to a target schema.
When to use
Use when decoding should change the schema's decoded type or encoded shape,
with an optional custom bidirectional transformation.
Details
Call it with the target schema to and then pipe the source schema from
into the returned function. The resulting schema decodes from
From["Encoded"] to To["Type"] and encodes from To["Type"] back to
From["Encoded"].
When no transformation is provided, SchemaTransformation.passthrough() is
used, so From["Type"] must already be compatible with To["Encoded"].
The resulting schema combines decoding and encoding services from both
schemas and any custom transformation.
Gotchas
In a custom transformation, decode maps From["Type"] to To["Encoded"]
and is used on the encoding path, while encode maps To["Encoded"] to
From["Type"] and is used on the decoding path.
Example (Transforming strings to numbers with a schema transformation)
import { Schema, SchemaGetter } from "effect"
const NumberFromString = Schema.String.pipe(
Schema.decodeTo(
Schema.Number,
{
decode: SchemaGetter.transform((s) => Number(s)),
encode: SchemaGetter.transform((n) => String(n))
}
)
)
const result = Schema.decodeUnknownSync(NumberFromString)("123")
// result: 123
Type-level representation returned by
decodeTo
.
decodeTo<interface Option<A extends Constraint>Schema for Option<A> values.
Type-level representation returned by
Option
.
Option<interface toType<S extends Constraint>Type-level representation returned by
toType
.
Extracts the type-side schema: sets Encoded to equal the decoded Type,
discarding the encoding transformation path.
toType<function (type parameter) S in OptionFromOptionalNullOr<S extends Constraint>S>>, interface optional<S extends Constraint>Type-level representation returned by
optional
.
Marks a struct field as optional, allowing the key to be absent or
undefined.
Details
The resulting property may be absent or explicitly set to undefined.
Equivalent to optionalKey(UndefinedOr(S)).
Use
optionalKey
instead if you want exact optional semantics (absent
only, not undefined).
Example (Defining an optional field accepting undefined)
import { Schema } from "effect"
const schema = Schema.Struct({
name: Schema.String,
age: Schema.optional(Schema.Number)
})
// { readonly name: string; readonly age?: number | undefined }
type Person = typeof schema.Type
optional<interface NullOr<S extends Constraint>Type-level representation returned by
NullOr
.
Creates a union schema of S | null.
NullOr<function (type parameter) S in OptionFromOptionalNullOr<S extends Constraint>S>>>
{
readonly "Rebuild": interface OptionFromOptionalNullOr<S extends Constraint>Decodes an optional or null or undefined value A to a required Option<A>
value.
Details
Decoding maps a missing key, undefined, or null to None, and maps all
other values to Some. Encoding maps Some to its value. None is encoded
according to options.onNoneEncoding: "omit" encodes a missing key,
null encodes null, and undefined encodes undefined.
Type-level representation returned by
OptionFromOptionalNullOr
.
OptionFromOptionalNullOr<function (type parameter) S in OptionFromOptionalNullOr<S extends Constraint>S>
}
/**
* Decodes an optional or `null` or `undefined` value `A` to a required `Option<A>`
* value.
*
* **Details**
*
* Decoding maps a missing key, `undefined`, or `null` to `None`, and maps all
* other values to `Some`. Encoding maps `Some` to its value. `None` is encoded
* according to `options.onNoneEncoding`: `"omit"` encodes a missing key,
* `null` encodes `null`, and `undefined` encodes `undefined`.
*
* @category Option
* @since 4.0.0
*/
export function function OptionFromOptionalNullOr<
S extends Constraint
>(
schema: S,
options?: {
readonly onNoneEncoding:
| "omit"
| null
| undefined
}
): OptionFromOptionalNullOr<S>
Decodes an optional or null or undefined value A to a required Option<A>
value.
Details
Decoding maps a missing key, undefined, or null to None, and maps all
other values to Some. Encoding maps Some to its value. None is encoded
according to options.onNoneEncoding: "omit" encodes a missing key,
null encodes null, and undefined encodes undefined.
OptionFromOptionalNullOr<function (type parameter) S in OptionFromOptionalNullOr<S extends Constraint>(schema: S, options?: {
readonly onNoneEncoding: "omit" | null | undefined;
}): OptionFromOptionalNullOr<S>
S extends Constraint>(
schema: S extends Constraintschema: function (type parameter) S in OptionFromOptionalNullOr<S extends Constraint>(schema: S, options?: {
readonly onNoneEncoding: "omit" | null | undefined;
}): OptionFromOptionalNullOr<S>
S,
options: | {
readonly onNoneEncoding:
| "omit"
| null
| undefined
}
| undefined
options?: {
readonly onNoneEncoding: "omit" | null | undefinedonNoneEncoding: "omit" | null | undefined
}
): interface OptionFromOptionalNullOr<S extends Constraint>Decodes an optional or null or undefined value A to a required Option<A>
value.
Details
Decoding maps a missing key, undefined, or null to None, and maps all
other values to Some. Encoding maps Some to its value. None is encoded
according to options.onNoneEncoding: "omit" encodes a missing key,
null encodes null, and undefined encodes undefined.
Type-level representation returned by
OptionFromOptionalNullOr
.
OptionFromOptionalNullOr<function (type parameter) S in OptionFromOptionalNullOr<S extends Constraint>(schema: S, options?: {
readonly onNoneEncoding: "omit" | null | undefined;
}): OptionFromOptionalNullOr<S>
S> {
const const onNoneEncoding:
| "omit"
| null
| undefined
onNoneEncoding = options: | {
readonly onNoneEncoding:
| "omit"
| null
| undefined
}
| undefined
options === var undefinedundefined ? "omit" : options: {
readonly onNoneEncoding:
| "omit"
| null
| undefined
}
options.onNoneEncoding: "omit" | null | undefinedonNoneEncoding
const const noneValue:
| S["Type"]
| null
| undefined
noneValue = const onNoneEncoding:
| "omit"
| null
| undefined
onNoneEncoding === null
? null as function (type parameter) S in OptionFromOptionalNullOr<S extends Constraint>(schema: S, options?: {
readonly onNoneEncoding: "omit" | null | undefined;
}): OptionFromOptionalNullOr<S>
S["Type"] | null | undefined
: var undefinedundefined as function (type parameter) S in OptionFromOptionalNullOr<S extends Constraint>(schema: S, options?: {
readonly onNoneEncoding: "omit" | null | undefined;
}): OptionFromOptionalNullOr<S>
S["Type"] | null | undefined
return const optional: optionalLambda
<NullOr<S>>(self: NullOr<S>) => optional<NullOr<S>>
Type-level representation returned by
optional
.
Marks a struct field as optional, allowing the key to be absent or
undefined.
Details
The resulting property may be absent or explicitly set to undefined.
Equivalent to optionalKey(UndefinedOr(S)).
Use
optionalKey
instead if you want exact optional semantics (absent
only, not undefined).
Example (Defining an optional field accepting undefined)
import { Schema } from "effect"
const schema = Schema.Struct({
name: Schema.String,
age: Schema.optional(Schema.Number)
})
// { readonly name: string; readonly age?: number | undefined }
type Person = typeof schema.Type
optional(const NullOr: NullOrLambda
;<S>(self: S) => NullOr<S>
Type-level representation returned by
NullOr
.
Creates a union schema of S | null.
NullOr(schema: S extends Constraintschema)).Pipeable.pipe<optional<NullOr<S>>, decodeTo<Option<toType<S>>, optional<NullOr<S>>, never, never>>(this: optional<NullOr<S>>, ab: (_: optional<NullOr<S>>) => decodeTo<Option<toType<S>>, optional<NullOr<S>>, never, never>): decodeTo<Option<toType<S>>, optional<NullOr<S>>, never, never> (+21 overloads)pipe(function decodeTo<Option<toType<S>>, optional<NullOr<S>>, never, never>(to: Option<toType<S>>, transformation: {
readonly decode: SchemaGetter.Getter<NoInfer<Option_.Option<S["Type"]>>, NoInfer<S["Type"] | null | undefined>, never>;
readonly encode: SchemaGetter.Getter<NoInfer<S["Type"] | ... 1 more ... | undefined>, NoInfer<Option_.Option<...>>, never>;
}): (from: optional<...>) => decodeTo<...> (+1 overload)
Creates a schema that transforms from a source schema to a target schema.
When to use
Use when decoding should change the schema's decoded type or encoded shape,
with an optional custom bidirectional transformation.
Details
Call it with the target schema to and then pipe the source schema from
into the returned function. The resulting schema decodes from
From["Encoded"] to To["Type"] and encodes from To["Type"] back to
From["Encoded"].
When no transformation is provided, SchemaTransformation.passthrough() is
used, so From["Type"] must already be compatible with To["Encoded"].
The resulting schema combines decoding and encoding services from both
schemas and any custom transformation.
Gotchas
In a custom transformation, decode maps From["Type"] to To["Encoded"]
and is used on the encoding path, while encode maps To["Encoded"] to
From["Type"] and is used on the decoding path.
Example (Transforming strings to numbers with a schema transformation)
import { Schema, SchemaGetter } from "effect"
const NumberFromString = Schema.String.pipe(
Schema.decodeTo(
Schema.Number,
{
decode: SchemaGetter.transform((s) => Number(s)),
encode: SchemaGetter.transform((n) => String(n))
}
)
)
const result = Schema.decodeUnknownSync(NumberFromString)("123")
// result: 123
decodeTo(
function Option<A extends Constraint>(
value: A
): Option<A>
Schema for Option<A> values.
Option(const toType: toTypeLambda
;<S>(self: S) => toType<S>
Type-level representation returned by
toType
.
Extracts the type-side schema: sets Encoded to equal the decoded Type,
discarding the encoding transformation path.
toType(schema: S extends Constraintschema)),
import SchemaTransformationSchemaTransformation.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 Option_Option_.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) S in OptionFromOptionalNullOr<S extends Constraint>(schema: S, options?: {
readonly onNoneEncoding: "omit" | null | undefined;
}): OptionFromOptionalNullOr<S>
S["Type"]>, function (type parameter) S in OptionFromOptionalNullOr<S extends Constraint>(schema: S, options?: {
readonly onNoneEncoding: "omit" | null | undefined;
}): OptionFromOptionalNullOr<S>
S["Type"] | null | undefined>({
decode: (
input: Option_.Option<
S["Type"] | null | undefined
>
) => Option_.Option<Option_.Option<S["Type"]>>
decode: (oe: Option_.Option<
S["Type"] | null | undefined
>
oe) => oe: Option_.Option<
S["Type"] | null | undefined
>
oe.Pipeable.pipe<Option_.Option<S["Type"] | null | undefined>, Option_.Option<S["Type"] & {}>, Option_.Option<Option_.Option<S["Type"] & {}>>>(this: Option_.Option<S["Type"] | null | undefined>, ab: (_: Option_.Option<S["Type"] | null | undefined>) => Option_.Option<S["Type"] & {}>, bc: (_: Option_.Option<S["Type"] & {}>) => Option_.Option<Option_.Option<S["Type"] & {}>>): Option_.Option<Option_.Option<S["Type"] & {}>> (+21 overloads)pipe(import Option_Option_.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 isNotNullish<A>(
input: A
): input is NonNullable<A>
Checks whether a value is not null and not undefined.
When to use
Use when you need a Predicate refinement that filters out nullish values
but keeps other falsy ones.
Details
Uses input != null.
Example (Filtering non-nullish values)
import { Predicate } from "effect"
const values = [0, null, "", undefined]
const present = values.filter(Predicate.isNotNullish)
console.log(present)
isNotNullish), import Option_Option_.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: (
input: Option_.Option<Option_.Option<S["Type"]>>
) => Option_.Option<S["Type"] | null | undefined>
encode: const onNoneEncoding:
| "omit"
| null
| undefined
onNoneEncoding === "omit"
? import Option_Option_.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
: (ot: Option_.Option<
Option_.Option<S["Type"]>
>
ot) => import Option_Option_.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(import Option_Option_.const getOrElse: {
<B>(onNone: LazyArg<B>): <A>(
self: Option<A>
) => B | A
<A, B>(self: Option<A>, onNone: LazyArg<B>):
| A
| B
}
getOrElse(import Option_Option_.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(ot: Option_.Option<
Option_.Option<S["Type"]>
>
ot), () => const noneValue:
| S["Type"]
| null
| undefined
noneValue))
})
))
}