withConstructorDefault<S>Attaches a constructor default value to a schema field.
Details
Constructor defaults are applied only during make*, not during decoding or
encoding.
Example (Defining an optional field with a static default)
import { Effect, Schema } from "effect"
const MySchema = Schema.Struct({
name: Schema.String.pipe(
Schema.optionalKey,
Schema.withConstructorDefault(Effect.succeed("anonymous"))
)
})
const value = MySchema.make({})
// value: { name: "anonymous" }export interface interface withConstructorDefault<S extends Constraint & WithoutConstructorDefault>Attaches a constructor default value to a schema field.
Details
Constructor defaults are applied only during make*, not during decoding or
encoding.
Example (Defining an optional field with a static default)
import { Effect, Schema } from "effect"
const MySchema = Schema.Struct({
name: Schema.String.pipe(
Schema.optionalKey,
Schema.withConstructorDefault(Effect.succeed("anonymous"))
)
})
const value = MySchema.make({})
// value: { name: "anonymous" }
Type-level representation returned by
withConstructorDefault
.
withConstructorDefault<function (type parameter) S in withConstructorDefault<S extends Constraint & WithoutConstructorDefault>S extends Constraint & WithoutConstructorDefault> extends
interface BottomLazy<out Ast extends SchemaAST.AST, out Rebuild extends Top, in out TypeParameters extends ReadonlyArray<Constraint> = readonly [], out TypeMutability extends Mutability = "readonly", out TypeOptionality extends Optionality = "required", out TypeConstructorDefault extends ConstructorDefault = "no-default", out EncodedMutability extends Mutability = "readonly", out EncodedOptionality extends Optionality = "required">Lazy Bottom variant for schema implementations that compute their public
views on demand.
When to use
Use as an implementation base for schema interfaces that must expose
Bottom behavior without forcing TypeScript to eagerly evaluate expensive
Type, Encoded, or service views.
Details
The laziness is purely type-level; runtime behavior is unchanged.
BottomLazy keeps the structural operations inherited from Bottom, but
erases the expensive schema views to unknown. Concrete schema interfaces can
then redeclare the precise views they expose. This keeps wide schemas such as
Struct and Union cheaper when generic code reads a single view, while
preserving their exact public types.
BottomLazy<
function (type parameter) S in withConstructorDefault<S extends Constraint & WithoutConstructorDefault>S["ast"],
interface withConstructorDefault<S extends Constraint & WithoutConstructorDefault>Attaches a constructor default value to a schema field.
Details
Constructor defaults are applied only during make*, not during decoding or
encoding.
Example (Defining an optional field with a static default)
import { Effect, Schema } from "effect"
const MySchema = Schema.Struct({
name: Schema.String.pipe(
Schema.optionalKey,
Schema.withConstructorDefault(Effect.succeed("anonymous"))
)
})
const value = MySchema.make({})
// value: { name: "anonymous" }
Type-level representation returned by
withConstructorDefault
.
withConstructorDefault<function (type parameter) S in withConstructorDefault<S extends Constraint & WithoutConstructorDefault>S>,
function (type parameter) S in withConstructorDefault<S extends Constraint & WithoutConstructorDefault>S["~type.parameters"],
function (type parameter) S in withConstructorDefault<S extends Constraint & WithoutConstructorDefault>S["~type.mutability"],
function (type parameter) S in withConstructorDefault<S extends Constraint & WithoutConstructorDefault>S["~type.optionality"],
"with-default",
function (type parameter) S in withConstructorDefault<S extends Constraint & WithoutConstructorDefault>S["~encoded.mutability"],
function (type parameter) S in withConstructorDefault<S extends Constraint & WithoutConstructorDefault>S["~encoded.optionality"]
>
{
readonly "Type": function (type parameter) S in withConstructorDefault<S extends Constraint & WithoutConstructorDefault>S["Type"]
readonly "Encoded": function (type parameter) S in withConstructorDefault<S extends Constraint & WithoutConstructorDefault>S["Encoded"]
readonly "DecodingServices": function (type parameter) S in withConstructorDefault<S extends Constraint & WithoutConstructorDefault>S["DecodingServices"]
readonly "EncodingServices": function (type parameter) S in withConstructorDefault<S extends Constraint & WithoutConstructorDefault>S["EncodingServices"]
readonly "~type.make.in": function (type parameter) S in withConstructorDefault<S extends Constraint & WithoutConstructorDefault>S["~type.make.in"]
readonly "~type.make": function (type parameter) S in withConstructorDefault<S extends Constraint & WithoutConstructorDefault>S["~type.make"]
readonly "Iso": function (type parameter) S in withConstructorDefault<S extends Constraint & WithoutConstructorDefault>S["Iso"]
readonly withConstructorDefault<S extends Constraint & WithoutConstructorDefault>.schema: S extends Constraint & WithoutConstructorDefaultschema: function (type parameter) S in withConstructorDefault<S extends Constraint & WithoutConstructorDefault>S
}
/**
* Attaches a constructor default value to a schema field.
*
* **Details**
*
* Constructor defaults are applied only during `make*`, not during decoding or
* encoding.
*
* **Example** (Defining an optional field with a static default)
*
* ```ts
* import { Effect, Schema } from "effect"
*
* const MySchema = Schema.Struct({
* name: Schema.String.pipe(
* Schema.optionalKey,
* Schema.withConstructorDefault(Effect.succeed("anonymous"))
* )
* })
*
* const value = MySchema.make({})
* // value: { name: "anonymous" }
* ```
*
* @category constructors
* @since 3.10.0
*/
export function function withConstructorDefault<
S extends Constraint & WithoutConstructorDefault
>(
defaultValue: Effect.Effect<
S["~type.make.in"],
SchemaError
>
): (schema: S) => withConstructorDefault<S>
Attaches a constructor default value to a schema field.
Details
Constructor defaults are applied only during make*, not during decoding or
encoding.
Example (Defining an optional field with a static default)
import { Effect, Schema } from "effect"
const MySchema = Schema.Struct({
name: Schema.String.pipe(
Schema.optionalKey,
Schema.withConstructorDefault(Effect.succeed("anonymous"))
)
})
const value = MySchema.make({})
// value: { name: "anonymous" }
withConstructorDefault<function (type parameter) S in withConstructorDefault<S extends Constraint & WithoutConstructorDefault>(defaultValue: Effect.Effect<S["~type.make.in"], SchemaError>): (schema: S) => withConstructorDefault<S>S extends Constraint & WithoutConstructorDefault>(
// `S["~type.make.in"]` instead of `S["Type"]` is intentional here because
// it makes easier to define the default value if there are nested defaults
defaultValue: Effect.Effect<
S["~type.make.in"],
SchemaError
>
(parameter) defaultValue: {
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;
}
defaultValue: import EffectEffect.type Effect.Effect = /*unresolved*/ anyEffect<function (type parameter) S in withConstructorDefault<S extends Constraint & WithoutConstructorDefault>(defaultValue: Effect.Effect<S["~type.make.in"], SchemaError>): (schema: S) => withConstructorDefault<S>S["~type.make.in"], class SchemaErrorclass SchemaError {
message: string;
toString: () => string;
name: string;
stack: string;
cause: unknown;
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; <…;
toJSON: () => unknown;
_tag: Tag;
issue: SchemaIssue.Issue;
}
Error thrown (or returned as the error channel value) when schema decoding
or encoding fails.
Details
The issue field contains a structured
Issue
tree describing
every validation failure, including the path to the problematic value,
expected types, and actual values received. message renders the issue tree
as a human-readable string.
Use
isSchemaError
to narrow an unknown value to SchemaError.
Example (Catching a SchemaError)
import { Schema } from "effect"
try {
Schema.decodeUnknownSync(Schema.Number)("not a number")
} catch (err) {
if (Schema.isSchemaError(err)) {
console.log(err.message)
// Expected number, actual "not a number"
}
}
SchemaError>
) {
return (schema: S extends Constraint & WithoutConstructorDefaultschema: function (type parameter) S in withConstructorDefault<S extends Constraint & WithoutConstructorDefault>(defaultValue: Effect.Effect<S["~type.make.in"], SchemaError>): (schema: S) => withConstructorDefault<S>S): interface withConstructorDefault<S extends Constraint & WithoutConstructorDefault>Attaches a constructor default value to a schema field.
Details
Constructor defaults are applied only during make*, not during decoding or
encoding.
Example (Defining an optional field with a static default)
import { Effect, Schema } from "effect"
const MySchema = Schema.Struct({
name: Schema.String.pipe(
Schema.optionalKey,
Schema.withConstructorDefault(Effect.succeed("anonymous"))
)
})
const value = MySchema.make({})
// value: { name: "anonymous" }
Type-level representation returned by
withConstructorDefault
.
withConstructorDefault<function (type parameter) S in withConstructorDefault<S extends Constraint & WithoutConstructorDefault>(defaultValue: Effect.Effect<S["~type.make.in"], SchemaError>): (schema: S) => withConstructorDefault<S>S> =>
const make: <S extends Constraint>(
ast: S["ast"],
options?: object
) => S
Creates a schema from an AST (Abstract Syntax Tree) node.
Details
This is the fundamental constructor for all schemas in the Effect Schema
library. It takes an AST node and wraps it in a fully-typed schema that
preserves all type information and provides the complete schema API.
The make function is used internally to create all primitive schemas like
String, Number, Boolean, etc., as well as more complex schemas. It's
the bridge between the untyped AST representation and the strongly-typed
schema.
make(import SchemaASTSchemaAST.function withConstructorDefault<
A extends AST
>(
ast: A,
defaultValue: Effect.Effect<
unknown,
SchemaIssue.Issue
>
): A
withConstructorDefault(schema: S extends Constraint & WithoutConstructorDefaultschema.Constraint["ast"]: SchemaAST.ASTast, function toIssueEffect<A, R>(
self: Effect.Effect<A, SchemaError, R>
): Effect.Effect<A, SchemaIssue.Issue, R>
toIssueEffect(defaultValue: Effect.Effect<
S["~type.make.in"],
SchemaError
>
(parameter) defaultValue: {
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;
}
defaultValue)), { schema: S extends Constraint & WithoutConstructorDefaultschema })
}