Skip to content

model

2 overloads

model<T extends object>(schema: intersection): ModelTokenBuilder<T>

Creates an extendable schema token for naming and authoring model classes.

Two calling forms are supported:

From a descriptor - define field shapes inline:

class UserModel extends model({
id: uuid,
name: string,
}) {}

From an existing schema - promote a schema token to a model token:

const UserSchema = schema({ id: uuid, name: string });
class UserModel extends model(UserSchema) {}

Discriminated unions have no model form: a class cannot carry a union instance type, so the extendable face model() exists for is unavailable to them. Use schema<T, "union">(discriminant, branches) directly; a union-typed schema token is rejected at the model() call site.

The returned token also satisfies SchemaToken<T>, so it can be used as a nested field value inside other schema descriptors.

schema intersection

returns ModelTokenBuilder<T>

model<T extends object>(descriptor: mapped): ModelTokenBuilder<T>

Creates an extendable schema token for naming and authoring model classes.

Two calling forms are supported:

From a descriptor - define field shapes inline:

class UserModel extends model({
id: uuid,
name: string,
}) {}

From an existing schema - promote a schema token to a model token:

const UserSchema = schema({ id: uuid, name: string });
class UserModel extends model(UserSchema) {}

Discriminated unions have no model form: a class cannot carry a union instance type, so the extendable face model() exists for is unavailable to them. Use schema<T, "union">(discriminant, branches) directly; a union-typed schema token is rejected at the model() call site.

The returned token also satisfies SchemaToken<T>, so it can be used as a nested field value inside other schema descriptors.

descriptor mapped

returns ModelTokenBuilder<T>

Learn: Models (DTOs)

Last updated: