<Self = never, Brand = {}>(identifier: string): {
<const Fields extends Struct.Fields>(
fields: Fields,
annotations?: Annotations.Declaration<Self, readonly [Struct<Fields>]>
): [Self] extends [never]
? MissingSelfGeneric<"Schema.ErrorClass">
: Class<Self, Struct<Fields>, Cause_.YieldableError & Brand>
<S extends Struct<Struct.Fields>>(
schema: S,
annotations?: Annotations.Declaration<Self, readonly [S]>
): [Self] extends [never]
? MissingSelfGeneric<"Schema.ErrorClass">
: Class<Self, S, Cause_.YieldableError & Brand>
}Creates a schema-backed error class that can be used as a typed,
yieldable error in Effect programs. Combines Class validation with
the YieldableError interface so instances can be yielded directly inside
Effect.gen.
Example (Schema-backed error)
import { Effect, Schema } from "effect"
class NotFound extends Schema.ErrorClass<NotFound>("NotFound")({
id: Schema.Number
}) {}
const program = Effect.gen(function*() {
yield* new NotFound({ id: 1 })
})export const const ErrorClass: {
<Self = never, Brand = {}>(
identifier: string
): {
<Fields extends Struct.Fields>(
fields: Fields,
annotations?: Annotations.Declaration<
Self,
readonly [Struct<Fields>]
>
): [Self] extends [never]
? MissingSelfGeneric<"Schema.ErrorClass">
: Class<
Self,
Struct<Fields>,
Cause_.YieldableError & Brand
>
<S extends Struct<Struct.Fields>>(
schema: S,
annotations?: Annotations.Declaration<
Self,
readonly [S]
>
): [Self] extends [never]
? MissingSelfGeneric<"Schema.ErrorClass">
: Class<
Self,
S,
Cause_.YieldableError & Brand
>
}
}
Creates a schema-backed error class that can be used as a typed,
yieldable error in Effect programs. Combines
Class
validation with
the YieldableError interface so instances can be yielded directly inside
Effect.gen.
Example (Schema-backed error)
import { Effect, Schema } from "effect"
class NotFound extends Schema.ErrorClass<NotFound>("NotFound")({
id: Schema.Number
}) {}
const program = Effect.gen(function*() {
yield* new NotFound({ id: 1 })
})
ErrorClass: {
<function (type parameter) Self in <Self = never, Brand = {}>(identifier: string): {
<const Fields extends Struct.Fields>(fields: Fields, annotations?: Annotations.Declaration<Self, readonly [Struct<Fields>]>): [Self] extends [never] ? MissingSelfGeneric<"Schema.ErrorClass"> : Class<Self, Struct<Fields>, Cause_.YieldableError & Brand>;
<S extends Struct<Struct.Fields>>(schema: S, annotations?: Annotations.Declaration<Self, readonly [S]>): [Self] extends [never] ? MissingSelfGeneric<"Schema.ErrorClass"> : Class<Self, S, Cause_.YieldableError & Brand>;
}
Self = never, function (type parameter) Brand in <Self = never, Brand = {}>(identifier: string): {
<const Fields extends Struct.Fields>(fields: Fields, annotations?: Annotations.Declaration<Self, readonly [Struct<Fields>]>): [Self] extends [never] ? MissingSelfGeneric<"Schema.ErrorClass"> : Class<Self, Struct<Fields>, Cause_.YieldableError & Brand>;
<S extends Struct<Struct.Fields>>(schema: S, annotations?: Annotations.Declaration<Self, readonly [S]>): [Self] extends [never] ? MissingSelfGeneric<"Schema.ErrorClass"> : Class<Self, S, Cause_.YieldableError & Brand>;
}
Brand = {}>(identifier: stringidentifier: string): {
<const function (type parameter) Fields in <const Fields extends Struct.Fields>(fields: Fields, annotations?: Annotations.Declaration<Self, readonly [Struct<Fields>]>): [Self] extends [never] ? MissingSelfGeneric<"Schema.ErrorClass"> : Class<Self, Struct<Fields>, Cause_.YieldableError & Brand>Fields extends Struct.type Struct<Fields extends Struct.Fields>.Fields = {
readonly [x: string]: Constraint;
readonly [x: number]: Constraint;
readonly [x: symbol]: Constraint;
}
Constraint for a struct field map: an object whose values are schemas.
Fields>(
fields: const Fields extends Struct.Fieldsfields: function (type parameter) Fields in <const Fields extends Struct.Fields>(fields: Fields, annotations?: Annotations.Declaration<Self, readonly [Struct<Fields>]>): [Self] extends [never] ? MissingSelfGeneric<"Schema.ErrorClass"> : Class<Self, Struct<Fields>, Cause_.YieldableError & Brand>Fields,
annotations: Annotations.Declaration<
Self,
readonly [Struct<Fields>]
>
annotations?: Annotations.interface Annotations.Declaration<T, TypeParameters extends ReadonlyArray<Constraint> = readonly []>Full annotation set for Declaration schema nodes — used when defining
custom, opaque schema types via Schema.declare. Extends
Bottom
with optional codec, arbitrary, equivalence, and formatter hooks so that
derived capabilities (JSON encoding, property testing, etc.) can be
provided for the custom type.
Declaration<function (type parameter) Self in <Self = never, Brand = {}>(identifier: string): {
<const Fields extends Struct.Fields>(fields: Fields, annotations?: Annotations.Declaration<Self, readonly [Struct<Fields>]>): [Self] extends [never] ? MissingSelfGeneric<"Schema.ErrorClass"> : Class<Self, Struct<Fields>, Cause_.YieldableError & Brand>;
<S extends Struct<Struct.Fields>>(schema: S, annotations?: Annotations.Declaration<Self, readonly [S]>): [Self] extends [never] ? MissingSelfGeneric<"Schema.ErrorClass"> : Class<Self, S, Cause_.YieldableError & Brand>;
}
Self, readonly [interface Struct<Fields extends Struct.Fields>Defines a struct schema from a map of field schemas.
Details
Each field value is a schema. Use
optionalKey
or
optional
to
mark fields as optional, and
mutableKey
to mark them as mutable.
The resulting schema's Type is a readonly object type with the fields'
decoded types. The Encoded form mirrors the field schemas' encoded types.
Example (Defining a basic struct)
import { Schema } from "effect"
const Person = Schema.Struct({
name: Schema.String,
age: Schema.Number,
email: Schema.optionalKey(Schema.String)
})
// { readonly name: string; readonly age: number; readonly email?: string }
type Person = typeof Person.Type
const alice = Schema.decodeUnknownSync(Person)({ name: "Alice", age: 30 })
console.log(alice)
// { name: 'Alice', age: 30 }
Namespace for struct field type utilities.
Details
These types compute the decoded Type, encoded Encoded, and constructor
input MakeIn of a
Struct
from its field map, handling optional,
mutable, and other field modifiers automatically.
Struct.Fields — constraint for the field map object
Struct.Type<F> — decoded type of the struct
Struct.Encoded<F> — encoded type of the struct
Struct.MakeIn<F> — constructor input (optional/defaulted fields may be omitted)
Struct.DecodingServices<F> / Struct.EncodingServices<F> — required services
Type-level representation returned by
Struct
.
Struct<function (type parameter) Fields in <const Fields extends Struct.Fields>(fields: Fields, annotations?: Annotations.Declaration<Self, readonly [Struct<Fields>]>): [Self] extends [never] ? MissingSelfGeneric<"Schema.ErrorClass"> : Class<Self, Struct<Fields>, Cause_.YieldableError & Brand>Fields>]>
): [function (type parameter) Self in <Self = never, Brand = {}>(identifier: string): {
<const Fields extends Struct.Fields>(fields: Fields, annotations?: Annotations.Declaration<Self, readonly [Struct<Fields>]>): [Self] extends [never] ? MissingSelfGeneric<"Schema.ErrorClass"> : Class<Self, Struct<Fields>, Cause_.YieldableError & Brand>;
<S extends Struct<Struct.Fields>>(schema: S, annotations?: Annotations.Declaration<Self, readonly [S]>): [Self] extends [never] ? MissingSelfGeneric<"Schema.ErrorClass"> : Class<Self, S, Cause_.YieldableError & Brand>;
}
Self] extends [never] ? type MissingSelfGeneric<
Usage extends string
> =
`Missing \`Self\` generic - use \`class Self extends ${Usage}<Self>(...)\``
MissingSelfGeneric<"Schema.ErrorClass">
: interface Class<Self, S extends Constraint & { readonly fields: Struct.Fields; }, Inherited>Type-level representation returned by
Class
.
Creates a schema-backed class whose constructor validates input against a
Struct
schema. Construction throws a
SchemaError
on invalid
input.
When to use
Use when you need a schema-backed data class with validated construction,
schema-derived decoding/encoding, and class-style methods or inheritance.
Details
Pass the desired class type as the first type parameter. The second optional
type parameter can be used to add nominal brands.
Gotchas
Passing disableChecks in the options skips constructor validation.
Example (Defining a basic class)
import { Schema } from "effect"
class Person extends Schema.Class<Person>("Person")({
name: Schema.String,
age: Schema.Number
}) {}
const alice = new Person({ name: "Alice", age: 30 })
console.log(alice.name) // "Alice"
console.log(`${alice}`) // "Person({ name: Alice, age: 30 })"
Example (Extending a class)
import { Schema } from "effect"
class Animal extends Schema.Class<Animal>("Animal")({
name: Schema.String
}) {}
class Dog extends Animal.extend<Dog>("Dog")({
breed: Schema.String
}) {}
const dog = new Dog({ name: "Rex", breed: "Labrador" })
console.log(dog.name) // "Rex"
console.log(dog.breed) // "Labrador"
Class<function (type parameter) Self in <Self = never, Brand = {}>(identifier: string): {
<const Fields extends Struct.Fields>(fields: Fields, annotations?: Annotations.Declaration<Self, readonly [Struct<Fields>]>): [Self] extends [never] ? MissingSelfGeneric<"Schema.ErrorClass"> : Class<Self, Struct<Fields>, Cause_.YieldableError & Brand>;
<S extends Struct<Struct.Fields>>(schema: S, annotations?: Annotations.Declaration<Self, readonly [S]>): [Self] extends [never] ? MissingSelfGeneric<"Schema.ErrorClass"> : Class<Self, S, Cause_.YieldableError & Brand>;
}
Self, interface Struct<Fields extends Struct.Fields>Defines a struct schema from a map of field schemas.
Details
Each field value is a schema. Use
optionalKey
or
optional
to
mark fields as optional, and
mutableKey
to mark them as mutable.
The resulting schema's Type is a readonly object type with the fields'
decoded types. The Encoded form mirrors the field schemas' encoded types.
Example (Defining a basic struct)
import { Schema } from "effect"
const Person = Schema.Struct({
name: Schema.String,
age: Schema.Number,
email: Schema.optionalKey(Schema.String)
})
// { readonly name: string; readonly age: number; readonly email?: string }
type Person = typeof Person.Type
const alice = Schema.decodeUnknownSync(Person)({ name: "Alice", age: 30 })
console.log(alice)
// { name: 'Alice', age: 30 }
Namespace for struct field type utilities.
Details
These types compute the decoded Type, encoded Encoded, and constructor
input MakeIn of a
Struct
from its field map, handling optional,
mutable, and other field modifiers automatically.
Struct.Fields — constraint for the field map object
Struct.Type<F> — decoded type of the struct
Struct.Encoded<F> — encoded type of the struct
Struct.MakeIn<F> — constructor input (optional/defaulted fields may be omitted)
Struct.DecodingServices<F> / Struct.EncodingServices<F> — required services
Type-level representation returned by
Struct
.
Struct<function (type parameter) Fields in <const Fields extends Struct.Fields>(fields: Fields, annotations?: Annotations.Declaration<Self, readonly [Struct<Fields>]>): [Self] extends [never] ? MissingSelfGeneric<"Schema.ErrorClass"> : Class<Self, Struct<Fields>, Cause_.YieldableError & Brand>Fields>, import Cause_Cause_.type Cause_.YieldableError = /*unresolved*/ anyYieldableError & function (type parameter) Brand in <Self = never, Brand = {}>(identifier: string): {
<const Fields extends Struct.Fields>(fields: Fields, annotations?: Annotations.Declaration<Self, readonly [Struct<Fields>]>): [Self] extends [never] ? MissingSelfGeneric<"Schema.ErrorClass"> : Class<Self, Struct<Fields>, Cause_.YieldableError & Brand>;
<S extends Struct<Struct.Fields>>(schema: S, annotations?: Annotations.Declaration<Self, readonly [S]>): [Self] extends [never] ? MissingSelfGeneric<"Schema.ErrorClass"> : Class<Self, S, Cause_.YieldableError & Brand>;
}
Brand>
<function (type parameter) S in <S extends Struct<Struct.Fields>>(schema: S, annotations?: Annotations.Declaration<Self, readonly [S]>): [Self] extends [never] ? MissingSelfGeneric<"Schema.ErrorClass"> : Class<Self, S, Cause_.YieldableError & Brand>S extends interface Struct<Fields extends Struct.Fields>Defines a struct schema from a map of field schemas.
Details
Each field value is a schema. Use
optionalKey
or
optional
to
mark fields as optional, and
mutableKey
to mark them as mutable.
The resulting schema's Type is a readonly object type with the fields'
decoded types. The Encoded form mirrors the field schemas' encoded types.
Example (Defining a basic struct)
import { Schema } from "effect"
const Person = Schema.Struct({
name: Schema.String,
age: Schema.Number,
email: Schema.optionalKey(Schema.String)
})
// { readonly name: string; readonly age: number; readonly email?: string }
type Person = typeof Person.Type
const alice = Schema.decodeUnknownSync(Person)({ name: "Alice", age: 30 })
console.log(alice)
// { name: 'Alice', age: 30 }
Namespace for struct field type utilities.
Details
These types compute the decoded Type, encoded Encoded, and constructor
input MakeIn of a
Struct
from its field map, handling optional,
mutable, and other field modifiers automatically.
Struct.Fields — constraint for the field map object
Struct.Type<F> — decoded type of the struct
Struct.Encoded<F> — encoded type of the struct
Struct.MakeIn<F> — constructor input (optional/defaulted fields may be omitted)
Struct.DecodingServices<F> / Struct.EncodingServices<F> — required services
Type-level representation returned by
Struct
.
Struct<Struct.type Struct<Fields extends Struct.Fields>.Fields = {
readonly [x: string]: Constraint;
readonly [x: number]: Constraint;
readonly [x: symbol]: Constraint;
}
Constraint for a struct field map: an object whose values are schemas.
Fields>>(
schema: S extends Struct<Struct.Fields>schema: function (type parameter) S in <S extends Struct<Struct.Fields>>(schema: S, annotations?: Annotations.Declaration<Self, readonly [S]>): [Self] extends [never] ? MissingSelfGeneric<"Schema.ErrorClass"> : Class<Self, S, Cause_.YieldableError & Brand>S,
annotations: | Annotations.Declaration<Self, readonly [S]>
| undefined
annotations?: Annotations.interface Annotations.Declaration<T, TypeParameters extends ReadonlyArray<Constraint> = readonly []>Full annotation set for Declaration schema nodes — used when defining
custom, opaque schema types via Schema.declare. Extends
Bottom
with optional codec, arbitrary, equivalence, and formatter hooks so that
derived capabilities (JSON encoding, property testing, etc.) can be
provided for the custom type.
Declaration<function (type parameter) Self in <Self = never, Brand = {}>(identifier: string): {
<const Fields extends Struct.Fields>(fields: Fields, annotations?: Annotations.Declaration<Self, readonly [Struct<Fields>]>): [Self] extends [never] ? MissingSelfGeneric<"Schema.ErrorClass"> : Class<Self, Struct<Fields>, Cause_.YieldableError & Brand>;
<S extends Struct<Struct.Fields>>(schema: S, annotations?: Annotations.Declaration<Self, readonly [S]>): [Self] extends [never] ? MissingSelfGeneric<"Schema.ErrorClass"> : Class<Self, S, Cause_.YieldableError & Brand>;
}
Self, readonly [function (type parameter) S in <S extends Struct<Struct.Fields>>(schema: S, annotations?: Annotations.Declaration<Self, readonly [S]>): [Self] extends [never] ? MissingSelfGeneric<"Schema.ErrorClass"> : Class<Self, S, Cause_.YieldableError & Brand>S]>
): [function (type parameter) Self in <Self = never, Brand = {}>(identifier: string): {
<const Fields extends Struct.Fields>(fields: Fields, annotations?: Annotations.Declaration<Self, readonly [Struct<Fields>]>): [Self] extends [never] ? MissingSelfGeneric<"Schema.ErrorClass"> : Class<Self, Struct<Fields>, Cause_.YieldableError & Brand>;
<S extends Struct<Struct.Fields>>(schema: S, annotations?: Annotations.Declaration<Self, readonly [S]>): [Self] extends [never] ? MissingSelfGeneric<"Schema.ErrorClass"> : Class<Self, S, Cause_.YieldableError & Brand>;
}
Self] extends [never] ? type MissingSelfGeneric<
Usage extends string
> =
`Missing \`Self\` generic - use \`class Self extends ${Usage}<Self>(...)\``
MissingSelfGeneric<"Schema.ErrorClass"> : interface Class<Self, S extends Constraint & { readonly fields: Struct.Fields; }, Inherited>Type-level representation returned by
Class
.
Creates a schema-backed class whose constructor validates input against a
Struct
schema. Construction throws a
SchemaError
on invalid
input.
When to use
Use when you need a schema-backed data class with validated construction,
schema-derived decoding/encoding, and class-style methods or inheritance.
Details
Pass the desired class type as the first type parameter. The second optional
type parameter can be used to add nominal brands.
Gotchas
Passing disableChecks in the options skips constructor validation.
Example (Defining a basic class)
import { Schema } from "effect"
class Person extends Schema.Class<Person>("Person")({
name: Schema.String,
age: Schema.Number
}) {}
const alice = new Person({ name: "Alice", age: 30 })
console.log(alice.name) // "Alice"
console.log(`${alice}`) // "Person({ name: Alice, age: 30 })"
Example (Extending a class)
import { Schema } from "effect"
class Animal extends Schema.Class<Animal>("Animal")({
name: Schema.String
}) {}
class Dog extends Animal.extend<Dog>("Dog")({
breed: Schema.String
}) {}
const dog = new Dog({ name: "Rex", breed: "Labrador" })
console.log(dog.name) // "Rex"
console.log(dog.breed) // "Labrador"
Class<function (type parameter) Self in <Self = never, Brand = {}>(identifier: string): {
<const Fields extends Struct.Fields>(fields: Fields, annotations?: Annotations.Declaration<Self, readonly [Struct<Fields>]>): [Self] extends [never] ? MissingSelfGeneric<"Schema.ErrorClass"> : Class<Self, Struct<Fields>, Cause_.YieldableError & Brand>;
<S extends Struct<Struct.Fields>>(schema: S, annotations?: Annotations.Declaration<Self, readonly [S]>): [Self] extends [never] ? MissingSelfGeneric<"Schema.ErrorClass"> : Class<Self, S, Cause_.YieldableError & Brand>;
}
Self, function (type parameter) S in <S extends Struct<Struct.Fields>>(schema: S, annotations?: Annotations.Declaration<Self, readonly [S]>): [Self] extends [never] ? MissingSelfGeneric<"Schema.ErrorClass"> : Class<Self, S, Cause_.YieldableError & Brand>S, import Cause_Cause_.type Cause_.YieldableError = /*unresolved*/ anyYieldableError & function (type parameter) Brand in <Self = never, Brand = {}>(identifier: string): {
<const Fields extends Struct.Fields>(fields: Fields, annotations?: Annotations.Declaration<Self, readonly [Struct<Fields>]>): [Self] extends [never] ? MissingSelfGeneric<"Schema.ErrorClass"> : Class<Self, Struct<Fields>, Cause_.YieldableError & Brand>;
<S extends Struct<Struct.Fields>>(schema: S, annotations?: Annotations.Declaration<Self, readonly [S]>): [Self] extends [never] ? MissingSelfGeneric<"Schema.ErrorClass"> : Class<Self, S, Cause_.YieldableError & Brand>;
}
Brand>
}
} = <function (type parameter) Self in <Self, Brand = {}>(identifier: string): (schema: Struct.Fields | Struct<Struct.Fields>, annotations?: Annotations.Declaration<Self, readonly [Struct<Struct.Fields>]>) => [Self] extends [never] ? MissingSelfGeneric<"Schema.ErrorClass"> : Class<Self, Struct<Struct.Fields>, Cause_.YieldableError & Brand>Self, function (type parameter) Brand in <Self, Brand = {}>(identifier: string): (schema: Struct.Fields | Struct<Struct.Fields>, annotations?: Annotations.Declaration<Self, readonly [Struct<Struct.Fields>]>) => [Self] extends [never] ? MissingSelfGeneric<"Schema.ErrorClass"> : Class<Self, Struct<Struct.Fields>, Cause_.YieldableError & Brand>Brand = {}>(identifier: stringidentifier: string) =>
(
schema: Struct.Fields | Struct<Struct.Fields>schema: Struct.type Struct<Fields extends Struct.Fields>.Fields = {
readonly [x: string]: Constraint;
readonly [x: number]: Constraint;
readonly [x: symbol]: Constraint;
}
Constraint for a struct field map: an object whose values are schemas.
Fields | interface Struct<Fields extends Struct.Fields>Defines a struct schema from a map of field schemas.
Details
Each field value is a schema. Use
optionalKey
or
optional
to
mark fields as optional, and
mutableKey
to mark them as mutable.
The resulting schema's Type is a readonly object type with the fields'
decoded types. The Encoded form mirrors the field schemas' encoded types.
Example (Defining a basic struct)
import { Schema } from "effect"
const Person = Schema.Struct({
name: Schema.String,
age: Schema.Number,
email: Schema.optionalKey(Schema.String)
})
// { readonly name: string; readonly age: number; readonly email?: string }
type Person = typeof Person.Type
const alice = Schema.decodeUnknownSync(Person)({ name: "Alice", age: 30 })
console.log(alice)
// { name: 'Alice', age: 30 }
Namespace for struct field type utilities.
Details
These types compute the decoded Type, encoded Encoded, and constructor
input MakeIn of a
Struct
from its field map, handling optional,
mutable, and other field modifiers automatically.
Struct.Fields — constraint for the field map object
Struct.Type<F> — decoded type of the struct
Struct.Encoded<F> — encoded type of the struct
Struct.MakeIn<F> — constructor input (optional/defaulted fields may be omitted)
Struct.DecodingServices<F> / Struct.EncodingServices<F> — required services
Type-level representation returned by
Struct
.
Struct<Struct.type Struct<Fields extends Struct.Fields>.Fields = {
readonly [x: string]: Constraint;
readonly [x: number]: Constraint;
readonly [x: symbol]: Constraint;
}
Constraint for a struct field map: an object whose values are schemas.
Fields>,
annotations: Annotations.Declaration<
Self,
readonly [Struct<Struct.Fields>]
>
annotations?: Annotations.interface Annotations.Declaration<T, TypeParameters extends ReadonlyArray<Constraint> = readonly []>Full annotation set for Declaration schema nodes — used when defining
custom, opaque schema types via Schema.declare. Extends
Bottom
with optional codec, arbitrary, equivalence, and formatter hooks so that
derived capabilities (JSON encoding, property testing, etc.) can be
provided for the custom type.
Declaration<function (type parameter) Self in <Self, Brand = {}>(identifier: string): (schema: Struct.Fields | Struct<Struct.Fields>, annotations?: Annotations.Declaration<Self, readonly [Struct<Struct.Fields>]>) => [Self] extends [never] ? MissingSelfGeneric<"Schema.ErrorClass"> : Class<Self, Struct<Struct.Fields>, Cause_.YieldableError & Brand>Self, readonly [interface Struct<Fields extends Struct.Fields>Defines a struct schema from a map of field schemas.
Details
Each field value is a schema. Use
optionalKey
or
optional
to
mark fields as optional, and
mutableKey
to mark them as mutable.
The resulting schema's Type is a readonly object type with the fields'
decoded types. The Encoded form mirrors the field schemas' encoded types.
Example (Defining a basic struct)
import { Schema } from "effect"
const Person = Schema.Struct({
name: Schema.String,
age: Schema.Number,
email: Schema.optionalKey(Schema.String)
})
// { readonly name: string; readonly age: number; readonly email?: string }
type Person = typeof Person.Type
const alice = Schema.decodeUnknownSync(Person)({ name: "Alice", age: 30 })
console.log(alice)
// { name: 'Alice', age: 30 }
Namespace for struct field type utilities.
Details
These types compute the decoded Type, encoded Encoded, and constructor
input MakeIn of a
Struct
from its field map, handling optional,
mutable, and other field modifiers automatically.
Struct.Fields — constraint for the field map object
Struct.Type<F> — decoded type of the struct
Struct.Encoded<F> — encoded type of the struct
Struct.MakeIn<F> — constructor input (optional/defaulted fields may be omitted)
Struct.DecodingServices<F> / Struct.EncodingServices<F> — required services
Type-level representation returned by
Struct
.
Struct<Struct.type Struct<Fields extends Struct.Fields>.Fields = {
readonly [x: string]: Constraint;
readonly [x: number]: Constraint;
readonly [x: symbol]: Constraint;
}
Constraint for a struct field map: an object whose values are schemas.
Fields>]>
): [function (type parameter) Self in <Self, Brand = {}>(identifier: string): (schema: Struct.Fields | Struct<Struct.Fields>, annotations?: Annotations.Declaration<Self, readonly [Struct<Struct.Fields>]>) => [Self] extends [never] ? MissingSelfGeneric<"Schema.ErrorClass"> : Class<Self, Struct<Struct.Fields>, Cause_.YieldableError & Brand>Self] extends [never] ? type MissingSelfGeneric<
Usage extends string
> =
`Missing \`Self\` generic - use \`class Self extends ${Usage}<Self>(...)\``
MissingSelfGeneric<"Schema.ErrorClass">
: interface Class<Self, S extends Constraint & { readonly fields: Struct.Fields; }, Inherited>Type-level representation returned by
Class
.
Creates a schema-backed class whose constructor validates input against a
Struct
schema. Construction throws a
SchemaError
on invalid
input.
When to use
Use when you need a schema-backed data class with validated construction,
schema-derived decoding/encoding, and class-style methods or inheritance.
Details
Pass the desired class type as the first type parameter. The second optional
type parameter can be used to add nominal brands.
Gotchas
Passing disableChecks in the options skips constructor validation.
Example (Defining a basic class)
import { Schema } from "effect"
class Person extends Schema.Class<Person>("Person")({
name: Schema.String,
age: Schema.Number
}) {}
const alice = new Person({ name: "Alice", age: 30 })
console.log(alice.name) // "Alice"
console.log(`${alice}`) // "Person({ name: Alice, age: 30 })"
Example (Extending a class)
import { Schema } from "effect"
class Animal extends Schema.Class<Animal>("Animal")({
name: Schema.String
}) {}
class Dog extends Animal.extend<Dog>("Dog")({
breed: Schema.String
}) {}
const dog = new Dog({ name: "Rex", breed: "Labrador" })
console.log(dog.name) // "Rex"
console.log(dog.breed) // "Labrador"
Class<function (type parameter) Self in <Self, Brand = {}>(identifier: string): (schema: Struct.Fields | Struct<Struct.Fields>, annotations?: Annotations.Declaration<Self, readonly [Struct<Struct.Fields>]>) => [Self] extends [never] ? MissingSelfGeneric<"Schema.ErrorClass"> : Class<Self, Struct<Struct.Fields>, Cause_.YieldableError & Brand>Self, interface Struct<Fields extends Struct.Fields>Defines a struct schema from a map of field schemas.
Details
Each field value is a schema. Use
optionalKey
or
optional
to
mark fields as optional, and
mutableKey
to mark them as mutable.
The resulting schema's Type is a readonly object type with the fields'
decoded types. The Encoded form mirrors the field schemas' encoded types.
Example (Defining a basic struct)
import { Schema } from "effect"
const Person = Schema.Struct({
name: Schema.String,
age: Schema.Number,
email: Schema.optionalKey(Schema.String)
})
// { readonly name: string; readonly age: number; readonly email?: string }
type Person = typeof Person.Type
const alice = Schema.decodeUnknownSync(Person)({ name: "Alice", age: 30 })
console.log(alice)
// { name: 'Alice', age: 30 }
Namespace for struct field type utilities.
Details
These types compute the decoded Type, encoded Encoded, and constructor
input MakeIn of a
Struct
from its field map, handling optional,
mutable, and other field modifiers automatically.
Struct.Fields — constraint for the field map object
Struct.Type<F> — decoded type of the struct
Struct.Encoded<F> — encoded type of the struct
Struct.MakeIn<F> — constructor input (optional/defaulted fields may be omitted)
Struct.DecodingServices<F> / Struct.EncodingServices<F> — required services
Type-level representation returned by
Struct
.
Struct<Struct.type Struct<Fields extends Struct.Fields>.Fields = {
readonly [x: string]: Constraint;
readonly [x: number]: Constraint;
readonly [x: symbol]: Constraint;
}
Constraint for a struct field map: an object whose values are schemas.
Fields>, import Cause_Cause_.type Cause_.YieldableError = /*unresolved*/ anyYieldableError & function (type parameter) Brand in <Self, Brand = {}>(identifier: string): (schema: Struct.Fields | Struct<Struct.Fields>, annotations?: Annotations.Declaration<Self, readonly [Struct<Struct.Fields>]>) => [Self] extends [never] ? MissingSelfGeneric<"Schema.ErrorClass"> : Class<Self, Struct<Struct.Fields>, Cause_.YieldableError & Brand>Brand> =>
{
const const struct: Struct<Struct.Fields>const struct: {
Type: Struct.Type<Fields>;
Encoded: Struct.Encoded<Fields>;
DecodingServices: Struct.DecodingServices<Fields>;
EncodingServices: Struct.EncodingServices<Fields>;
Iso: Struct.Iso<Fields>;
fields: Fields;
mapFields: (f: (fields: Struct.Fields) => To, options?: { readonly unsafePreserveChecks?: boolean | undefined } | undefined) => Struct<{ [K in keyof Readonly<To>]: Readonly<To>[K]; }>;
Rebuild: Rebuild;
ast: Ast;
annotate: (annotations: Annotations.Bottom<Struct.ReadonlySide<Struct.Fields, 'Type'>, readonly []>) => Struct<Struct.Fields>;
annotateKey: (annotations: Annotations.Key<Struct.ReadonlySide<Struct.Fields, 'Type'>>) => Struct<Struct.Fields>;
check: (checks_0: SchemaAST.Check<Struct.ReadonlySide<Struct.Fields, 'Type'>>, ...checks: Array<SchemaAST.Check<Struct.ReadonlySide<Struct.Fields, 'Type'>>>) => Struct<Struct.Fields>;
rebuild: (ast: SchemaAST.Objects) => Struct<Struct.Fields>;
make: (input: Struct.ReadonlyMakeIn<Struct.Fields>, options?: MakeOptions) => Struct.ReadonlySide<Struct.Fields, 'Type'>;
makeOption: (input: Struct.ReadonlyMakeIn<Struct.Fields>, options?: MakeOptions) => Option_.Option<Struct.ReadonlySide<Struct.Fields, 'Type'>>;
makeEffect: (input: Struct.ReadonlyMakeIn<Struct.Fields>, options?: MakeOptions) => Effect.Effect<Struct.ReadonlySide<Struct.Fields, 'Type'>, SchemaError, never>;
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; <…;
}
struct = function isStruct(
schema: Struct.Fields | Struct<Struct.Fields>
): schema is Struct<Struct.Fields>
isStruct(schema: Struct.Fields | Struct<Struct.Fields>schema) ? schema: Struct.Fields | Struct<Struct.Fields>(parameter) schema: {
Type: Struct.Type<Fields>;
Encoded: Struct.Encoded<Fields>;
DecodingServices: Struct.DecodingServices<Fields>;
EncodingServices: Struct.EncodingServices<Fields>;
Iso: Struct.Iso<Fields>;
fields: Fields;
mapFields: (f: (fields: Struct.Fields) => To, options?: { readonly unsafePreserveChecks?: boolean | undefined } | undefined) => Struct<{ [K in keyof Readonly<To>]: Readonly<To>[K]; }>;
Rebuild: Rebuild;
ast: Ast;
annotate: (annotations: Annotations.Bottom<Struct.ReadonlySide<Struct.Fields, 'Type'>, readonly []>) => Struct<Struct.Fields>;
annotateKey: (annotations: Annotations.Key<Struct.ReadonlySide<Struct.Fields, 'Type'>>) => Struct<Struct.Fields>;
check: (checks_0: SchemaAST.Check<Struct.ReadonlySide<Struct.Fields, 'Type'>>, ...checks: Array<SchemaAST.Check<Struct.ReadonlySide<Struct.Fields, 'Type'>>>) => Struct<Struct.Fields>;
rebuild: (ast: SchemaAST.Objects) => Struct<Struct.Fields>;
make: (input: Struct.ReadonlyMakeIn<Struct.Fields>, options?: MakeOptions) => Struct.ReadonlySide<Struct.Fields, 'Type'>;
makeOption: (input: Struct.ReadonlyMakeIn<Struct.Fields>, options?: MakeOptions) => Option_.Option<Struct.ReadonlySide<Struct.Fields, 'Type'>>;
makeEffect: (input: Struct.ReadonlyMakeIn<Struct.Fields>, options?: MakeOptions) => Effect.Effect<Struct.ReadonlySide<Struct.Fields, 'Type'>, SchemaError, never>;
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; <…;
}
schema : function Struct<
Fields extends Struct.Fields
>(fields: Fields): Struct<Fields>
Defines a struct schema from a map of field schemas.
Details
Each field value is a schema. Use
optionalKey
or
optional
to
mark fields as optional, and
mutableKey
to mark them as mutable.
The resulting schema's Type is a readonly object type with the fields'
decoded types. The Encoded form mirrors the field schemas' encoded types.
Example (Defining a basic struct)
import { Schema } from "effect"
const Person = Schema.Struct({
name: Schema.String,
age: Schema.Number,
email: Schema.optionalKey(Schema.String)
})
// { readonly name: string; readonly age: number; readonly email?: string }
type Person = typeof Person.Type
const alice = Schema.decodeUnknownSync(Person)({ name: "Alice", age: 30 })
console.log(alice)
// { name: 'Alice', age: 30 }
Struct(schema: Struct.Fields | Struct<Struct.Fields>schema)
const const self: anyself = function makeClass<
Self,
S extends Struct<Struct.Fields>,
Inherited extends new (
...args: ReadonlyArray<any>
) => any
>(
Inherited: Inherited,
identifier: string,
struct: S,
annotations:
| Annotations.Declaration<Self, readonly [S]>
| undefined,
proto:
| ((identifier: string) => object)
| undefined
): any
makeClass(
import corecore.const Error: new <
A extends Record<string, any> = {}
>(
args: Types.VoidIfEmpty<{
readonly [P in keyof A]: A[P]
}>
) => Cause.YieldableError & Readonly<A>
Error,
identifier: stringidentifier,
const struct: Struct<Struct.Fields>const struct: {
Type: Struct.Type<Fields>;
Encoded: Struct.Encoded<Fields>;
DecodingServices: Struct.DecodingServices<Fields>;
EncodingServices: Struct.EncodingServices<Fields>;
Iso: Struct.Iso<Fields>;
fields: Fields;
mapFields: (f: (fields: Struct.Fields) => To, options?: { readonly unsafePreserveChecks?: boolean | undefined } | undefined) => Struct<{ [K in keyof Readonly<To>]: Readonly<To>[K]; }>;
Rebuild: Rebuild;
ast: Ast;
annotate: (annotations: Annotations.Bottom<Struct.ReadonlySide<Struct.Fields, 'Type'>, readonly []>) => Struct<Struct.Fields>;
annotateKey: (annotations: Annotations.Key<Struct.ReadonlySide<Struct.Fields, 'Type'>>) => Struct<Struct.Fields>;
check: (checks_0: SchemaAST.Check<Struct.ReadonlySide<Struct.Fields, 'Type'>>, ...checks: Array<SchemaAST.Check<Struct.ReadonlySide<Struct.Fields, 'Type'>>>) => Struct<Struct.Fields>;
rebuild: (ast: SchemaAST.Objects) => Struct<Struct.Fields>;
make: (input: Struct.ReadonlyMakeIn<Struct.Fields>, options?: MakeOptions) => Struct.ReadonlySide<Struct.Fields, 'Type'>;
makeOption: (input: Struct.ReadonlyMakeIn<Struct.Fields>, options?: MakeOptions) => Option_.Option<Struct.ReadonlySide<Struct.Fields, 'Type'>>;
makeEffect: (input: Struct.ReadonlyMakeIn<Struct.Fields>, options?: MakeOptions) => Effect.Effect<Struct.ReadonlySide<Struct.Fields, 'Type'>, SchemaError, never>;
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; <…;
}
struct,
annotations: Annotations.Declaration<
Self,
readonly [Struct<Struct.Fields>]
>
annotations,
(identifier: stringidentifier) => ({
name: stringname: identifier: stringidentifier
})
)
return const self: anyself
}