optional<S>Type-level representation returned by optional.
export interface 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<function (type parameter) S in optional<S extends Constraint>S extends Constraint> extends interface optionalKey<S extends Constraint>Type-level representation returned by
optionalKey
.
Creates an exact optional key schema for struct fields. Unlike optional,
this creates exact optional properties (not | undefined) that can be
completely omitted from the object.
Example (Creating a struct with optional key)
import { Schema } from "effect"
const schema = Schema.Struct({
name: Schema.String,
age: Schema.optionalKey(Schema.Number)
})
// Type: { readonly name: string; readonly age?: number }
type Person = typeof schema["Type"]
optionalKey<interface UndefinedOr<S extends Constraint>Type-level representation returned by
UndefinedOr
.
Creates a union schema of S | undefined.
UndefinedOr<function (type parameter) S in optional<S extends Constraint>S>> {
readonly "Rebuild": 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<function (type parameter) S in optional<S extends Constraint>S>
}
interface optionalLambda extends Lambda {
<function (type parameter) S in <S extends Constraint>(self: S): optional<S>S extends Constraint>(self: S extends Constraintself: function (type parameter) S in <S extends Constraint>(self: S): optional<S>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<function (type parameter) S in <S extends Constraint>(self: S): optional<S>S>
readonly "~lambda.out": this["~lambda.in"] extends Constraint ? 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<this["~lambda.in"]> : never
}
/**
* 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 {@link optionalKey} instead if you want exact optional semantics (absent
* only, not `undefined`).
*
* **Example** (Defining an optional field accepting undefined)
*
* ```ts
* 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
* ```
*
* @category combinators
* @since 3.10.0
*/
export const const optional: optionalLambdaType-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 = import Struct_Struct_.const lambda: <optionalLambda>(
f: (a: Constraint) => optional<Constraint>
) => optionalLambda
Wraps a plain function as a
Lambda
value so it can be used with
When to use
Use to create a typed lambda for struct mapping APIs that need type-level
input and output tracking.
Details
The type parameter L encodes both the input and output types at the type
level, allowing the compiler to track how struct value types change. At
runtime, the returned value is the same function; lambda only adjusts the
type.
Example (Wrapping values in arrays)
import { pipe, Struct } from "effect"
interface AsArray extends Struct.Lambda {
<A>(self: A): Array<A>
readonly "~lambda.out": Array<this["~lambda.in"]>
}
const asArray = Struct.lambda<AsArray>((a) => [a])
const result = pipe({ x: 1, y: "hello" }, Struct.map(asArray))
console.log(result) // { x: [1], y: ["hello"] }
lambda<optionalLambda>((self: Constraint(parameter) self: {
ast: SchemaAST.AST;
Type: unknown;
Encoded: unknown;
DecodingServices: unknown;
EncodingServices: unknown;
Iso: unknown;
}
self) => const optionalKey: optionalKeyLambda
<UndefinedOr<Constraint>>(self: UndefinedOr<Constraint>) => optionalKey<UndefinedOr<Constraint>>
Type-level representation returned by
optionalKey
.
Creates an exact optional key schema for struct fields. Unlike optional,
this creates exact optional properties (not | undefined) that can be
completely omitted from the object.
Example (Creating a struct with optional key)
import { Schema } from "effect"
const schema = Schema.Struct({
name: Schema.String,
age: Schema.optionalKey(Schema.Number)
})
// Type: { readonly name: string; readonly age?: number }
type Person = typeof schema["Type"]
optionalKey(const UndefinedOr: UndefinedOrLambda
;<Constraint>(self: Constraint) =>
UndefinedOr<Constraint>
Type-level representation returned by
UndefinedOr
.
Creates a union schema of S | undefined.
UndefinedOr(self: Constraint(parameter) self: {
ast: SchemaAST.AST;
Type: unknown;
Encoded: unknown;
DecodingServices: unknown;
EncodingServices: unknown;
Iso: unknown;
}
self)))