Skip to content

Regression: ZenStack v3 does not properly generate TypeScript string literal discriminators for delegated models #600

@ellie-xpawn

Description

@ellie-xpawn

Description and expected behavior
After migrating from ZenStack v2 to v3, there is no longer a type-safe way to discriminate TypeScript types generated from delegated models.

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: string // Expected: "Foo"
  foo: number
} | {
  id: string
  type: string // Expected: "Bar"
  bar: string
}

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

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

It's the same with enums as well - if an enum is used in place of String, then every submodel still will not narrow types as expected:

enum Type {
  Foo
  Bar
}

model Base {
  ...
  @@delegate(Type)
}
...
type Base = {
  id: string
  type: "Foo" | "Bar"
  foo: number
} | {
  id: string
  type: "Foo" | "Bar"
  bar: string
}

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

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

Because of this, Base cannot be narrowed to Foo or Bar without opting out of type checking and using a type assertion.

This didn't work entirely as expected in v2 either, but workarounds were possible. See zenstackhq/zenstack#2334 for more info.

Screenshots
If applicable, add screenshots to help explain your problem.

Environment (please complete the following information):

  • ZenStack version: 3.2.1
  • Database type: PostgreSQL

Additional context
Add any other context about the problem here.

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