OptionFromNullishOr<S>Decodes a nullish value T to a required Option<T> value.
Details
Decoding maps null and undefined to None and all other values to
Some. Encoding maps None to null or undefined depending on
options.onNoneEncoding, which defaults to undefined, and maps Some to
its value.
export interface interface OptionFromNullishOr<S extends Constraint>Decodes a nullish value T to a required Option<T> value.
Details
Decoding maps null and undefined to None and all other values to
Some. Encoding maps None to null or undefined depending on
options.onNoneEncoding, which defaults to undefined, and maps Some to
its value.
Type-level representation returned by
OptionFromNullishOr
.
OptionFromNullishOr<function (type parameter) S in OptionFromNullishOr<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 OptionFromNullishOr<S extends Constraint>S>>, interface NullishOr<S extends Constraint>Type-level representation returned by
NullishOr
.
Creates a union schema of S | null | undefined.
NullishOr<function (type parameter) S in OptionFromNullishOr<S extends Constraint>S>> {
readonly "Rebuild": interface OptionFromNullishOr<S extends Constraint>Decodes a nullish value T to a required Option<T> value.
Details
Decoding maps null and undefined to None and all other values to
Some. Encoding maps None to null or undefined depending on
options.onNoneEncoding, which defaults to undefined, and maps Some to
its value.
Type-level representation returned by
OptionFromNullishOr
.
OptionFromNullishOr<function (type parameter) S in OptionFromNullishOr<S extends Constraint>S>
}
/**
* Decodes a nullish value `T` to a required `Option<T>` value.
*
* **Details**
*
* Decoding maps `null` and `undefined` to `None` and all other values to
* `Some`. Encoding maps `None` to `null` or `undefined` depending on
* `options.onNoneEncoding`, which defaults to `undefined`, and maps `Some` to
* its value.
*
* @category Option
* @since 3.10.0
*/
export function function OptionFromNullishOr<
S extends Constraint
>(
schema: S,
options?: { onNoneEncoding: null | undefined }
): OptionFromNullishOr<S>
Decodes a nullish value T to a required Option<T> value.
Details
Decoding maps null and undefined to None and all other values to
Some. Encoding maps None to null or undefined depending on
options.onNoneEncoding, which defaults to undefined, and maps Some to
its value.
OptionFromNullishOr<function (type parameter) S in OptionFromNullishOr<S extends Constraint>(schema: S, options?: {
onNoneEncoding: null | undefined;
}): OptionFromNullishOr<S>
S extends Constraint>(
schema: S extends Constraintschema: function (type parameter) S in OptionFromNullishOr<S extends Constraint>(schema: S, options?: {
onNoneEncoding: null | undefined;
}): OptionFromNullishOr<S>
S,
options: | {
onNoneEncoding: null | undefined
}
| undefined
options?: {
onNoneEncoding: null | undefinedonNoneEncoding: null | undefined
}
): interface OptionFromNullishOr<S extends Constraint>Decodes a nullish value T to a required Option<T> value.
Details
Decoding maps null and undefined to None and all other values to
Some. Encoding maps None to null or undefined depending on
options.onNoneEncoding, which defaults to undefined, and maps Some to
its value.
Type-level representation returned by
OptionFromNullishOr
.
OptionFromNullishOr<function (type parameter) S in OptionFromNullishOr<S extends Constraint>(schema: S, options?: {
onNoneEncoding: null | undefined;
}): OptionFromNullishOr<S>
S> {
return const NullishOr: NullishOrLambda
;<S>(self: S) => NullishOr<S>
Type-level representation returned by
NullishOr
.
Creates a union schema of S | null | undefined.
NullishOr(schema: S extends Constraintschema).pipe(function decodeTo<Option<toType<S>>, Constraint, never, never>(to: Option<toType<S>>, transformation: {
readonly decode: SchemaGetter.Getter<Option_.Option<A["Encoded"]>, unknown, never>;
readonly encode: SchemaGetter.Getter<unknown, Option_.Option<A["Encoded"]>, never>;
}): (from: Constraint) => decodeTo<Option<toType<S>>, Constraint, never, never> (+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 optionFromNullishOr<
T
>(options?: {
onNoneEncoding: null | undefined
}): Transformation<
Option.Option<T>,
T | null | undefined
>
Decodes T | null | undefined into Option<T> and encodes Option<T>
back to T | null or T | undefined depending on the provided
options.onNoneEncoding (defaults to undefined).
When to use
Use when you need a schema transformation to convert nullish API fields to
Option when both null and undefined represent absence.
Details
Decoding maps null and undefined to Option.none() and all other values
to Option.some(value). Encoding maps Option.none() to null or
undefined according to options.onNoneEncoding, and maps
Option.some(value) to value. The transformation is pure and synchronous.
Example (Converting nullish values to an Option and encoding None as null)
import { Schema, SchemaTransformation } from "effect"
const schema = Schema.NullishOr(Schema.String).pipe(
Schema.decodeTo(
Schema.Option(Schema.String),
SchemaTransformation.optionFromNullishOr({ onNoneEncoding: null })
)
)
optionFromNullishOr(options: | {
onNoneEncoding: null | undefined
}
| undefined
options)
))
}