Effect CommunityEC
Effect Community2y ago
14 replies
Madyan

TypeScript: Defining a Type for `Schema.Class` with an `id` Field

How can I write a type that only accepts Schema.Class with an
id
field?
Here is my attempt but it's getting a compiler error:

const Cuid2 = String.pipe(pattern(/^[a-z][a-z0-9]*$/, { identifier: "Cuid2" }))
const ChatId = Cuid2.pipe(brand("ChatId"))
// Generated type: Schema.brand<Schema.filter<Schema.Schema<string, string, never>>, "ChatId">

export class Chat extends Class<Chat>("Chat")({
  id: ChatId,
  message: String,
}) {}

type Entity = Schema.Class<any, { id: Schema.Schema<string> }, any, any, any, any, any>
const processEntity = (entity: Entity) => entity.fields.id
processEntity(Chat)
Screenshot_2024-07-28_at_22.55.07.png
Was this page helpful?