Skip to content

Bug: ZenStack v2 loses type information in TypeScript types for delegated polymorphic models #2334

@ellie-xpawn

Description

@ellie-xpawn

Description and expected behavior
There is a mismatch between the TypeScript type generated for the base model and all of its delegated submodels.

Consider the following schema:

model Base {
  id String @id @default(cuid(2))
  type String
  @@delegate(type)
}

model Foo extends Base {
  foo Number
}

model Bar extends Base {
  bar String
}

The types generated are as follows:

type Base = {
  id: string
  type: "Foo"
  foo: number
} | {
  id: string
  type: "Bar"
  bar: string
}

type Foo = {
  id: string
  type: string // Expected: "Foo"
  foo: number
}

type Bar = {
  id: string
  type: string // Expected: "Bar"
  bar: string
}

The types of the submodels Foo and Bar lose their discriminator, while the Base union does not. As a workaround, I use the following pattern everywhere:

type Foo = Extract<Base, { type: "Foo" }>
type Bar = Extract<Base, { type: "Bar" }>

Environment (please complete the following information):

  • ZenStack version: 2.21.0
  • Prisma version: 6.18.0
  • Database type: PostgreSQL

Additional context

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions