Opaque<Self, S, Brand>Wraps a struct schema so that its decoded Type becomes a nominally distinct type Self.
Useful for creating opaque types that are structurally identical to a base struct
but type-incompatible with it.
Example (Defining opaque structs)
import { Schema } from "effect"
class Person extends Schema.Opaque<Person>()(
Schema.Struct({
name: Schema.String
})
) {}
// Decoded value is Person, not { name: string }
const person = Schema.decodeUnknownSync(Person)({ name: "Alice" })
// person: Personexport interface interface Opaque<Self, S extends Top, Brand>Wraps a struct schema so that its decoded Type becomes a nominally distinct type Self.
Useful for creating opaque types that are structurally identical to a base struct
but type-incompatible with it.
Example (Defining opaque structs)
import { Schema } from "effect"
class Person extends Schema.Opaque<Person>()(
Schema.Struct({
name: Schema.String
})
) {}
// Decoded value is Person, not { name: string }
const person = Schema.decodeUnknownSync(Person)({ name: "Alice" })
// person: Person
Type-level representation returned by
Opaque
.
Opaque<function (type parameter) Self in Opaque<Self, S extends Top, Brand>Self, function (type parameter) S in Opaque<Self, S extends Top, Brand>S extends Top, function (type parameter) Brand in Opaque<Self, S extends Top, Brand>Brand> 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 Opaque<Self, S extends Top, Brand>S["ast"],
function (type parameter) S in Opaque<Self, S extends Top, Brand>S["Rebuild"],
function (type parameter) S in Opaque<Self, S extends Top, Brand>S["~type.parameters"],
function (type parameter) S in Opaque<Self, S extends Top, Brand>S["~type.mutability"],
function (type parameter) S in Opaque<Self, S extends Top, Brand>S["~type.optionality"],
function (type parameter) S in Opaque<Self, S extends Top, Brand>S["~type.constructor.default"],
function (type parameter) S in Opaque<Self, S extends Top, Brand>S["~encoded.mutability"],
function (type parameter) S in Opaque<Self, S extends Top, Brand>S["~encoded.optionality"]
>
{
readonly "Type": function (type parameter) Self in Opaque<Self, S extends Top, Brand>Self
readonly "Encoded": function (type parameter) S in Opaque<Self, S extends Top, Brand>S["Encoded"]
readonly "DecodingServices": function (type parameter) S in Opaque<Self, S extends Top, Brand>S["DecodingServices"]
readonly "EncodingServices": function (type parameter) S in Opaque<Self, S extends Top, Brand>S["EncodingServices"]
readonly "~type.make.in": function (type parameter) S in Opaque<Self, S extends Top, Brand>S["~type.make.in"]
readonly "~type.make": function (type parameter) S in Opaque<Self, S extends Top, Brand>S["~type.make"]
readonly "Iso": function (type parameter) S in Opaque<Self, S extends Top, Brand>S["Iso"]
new(_: never_: never): function (type parameter) S in Opaque<Self, S extends Top, Brand>S["Type"] & function (type parameter) Brand in Opaque<Self, S extends Top, Brand>Brand
}
/**
* Wraps a struct schema so that its decoded `Type` becomes a nominally distinct type `Self`.
* Useful for creating opaque types that are structurally identical to a base struct
* but type-incompatible with it.
*
* **Example** (Defining opaque structs)
*
* ```ts
* import { Schema } from "effect"
*
* class Person extends Schema.Opaque<Person>()(
* Schema.Struct({
* name: Schema.String
* })
* ) {}
*
* // Decoded value is Person, not { name: string }
* const person = Schema.decodeUnknownSync(Person)({ name: "Alice" })
* // person: Person
* ```
*
* @category constructors
* @since 4.0.0
*/
export function function Opaque<Self, Brand = {}>(): <
S extends Top
>(
schema: S
) => Opaque<Self, S, Brand> & Omit<S, keyof Top>
Wraps a struct schema so that its decoded Type becomes a nominally distinct type Self.
Useful for creating opaque types that are structurally identical to a base struct
but type-incompatible with it.
Example (Defining opaque structs)
import { Schema } from "effect"
class Person extends Schema.Opaque<Person>()(
Schema.Struct({
name: Schema.String
})
) {}
// Decoded value is Person, not { name: string }
const person = Schema.decodeUnknownSync(Person)({ name: "Alice" })
// person: Person
Opaque<function (type parameter) Self in Opaque<Self, Brand = {}>(): <S extends Top>(schema: S) => Opaque<Self, S, Brand> & Omit<S, keyof Top>Self, function (type parameter) Brand in Opaque<Self, Brand = {}>(): <S extends Top>(schema: S) => Opaque<Self, S, Brand> & Omit<S, keyof Top>Brand = {}>() {
return <function (type parameter) S in <S extends Top>(schema: S): Opaque<Self, S, Brand> & Omit<S, keyof Top>S extends Top>(schema: S extends Topschema: function (type parameter) S in <S extends Top>(schema: S): Opaque<Self, S, Brand> & Omit<S, keyof Top>S): interface Opaque<Self, S extends Top, Brand>Wraps a struct schema so that its decoded Type becomes a nominally distinct type Self.
Useful for creating opaque types that are structurally identical to a base struct
but type-incompatible with it.
Example (Defining opaque structs)
import { Schema } from "effect"
class Person extends Schema.Opaque<Person>()(
Schema.Struct({
name: Schema.String
})
) {}
// Decoded value is Person, not { name: string }
const person = Schema.decodeUnknownSync(Person)({ name: "Alice" })
// person: Person
Type-level representation returned by
Opaque
.
Opaque<function (type parameter) Self in Opaque<Self, Brand = {}>(): <S extends Top>(schema: S) => Opaque<Self, S, Brand> & Omit<S, keyof Top>Self, function (type parameter) S in <S extends Top>(schema: S): Opaque<Self, S, Brand> & Omit<S, keyof Top>S, function (type parameter) Brand in Opaque<Self, Brand = {}>(): <S extends Top>(schema: S) => Opaque<Self, S, Brand> & Omit<S, keyof Top>Brand> & type Omit<T, K extends keyof any> = {
[P in Exclude<keyof T, K>]: T[P]
}
Construct a type with the properties of T except for those in type K.
Omit<function (type parameter) S in <S extends Top>(schema: S): Opaque<Self, S, Brand> & Omit<S, keyof Top>S, keyof Top> => {
// oxlint-disable-next-line @typescript-eslint/no-extraneous-class
class class OpaqueOpaque {}
return var Object: ObjectConstructorProvides functionality common to all JavaScript objects.
Object.ObjectConstructor.setPrototypeOf(o: any, proto: object | null): anySets the prototype of a specified object o to object proto or null. Returns the object o.
setPrototypeOf(class OpaqueOpaque, schema: S extends Topschema)
}
}