zenstackhq/zenstack

[BUG] Typescript asks for delegation field when creating new polymorphic instance and providing id with a default of auto increment

Closed this issue · 2 comments

Description and expected behavior
Typescript asks for delegation field when creating new polymorphic instance when providing id with a default of auto increment.

Screenshots
image

Steps to reproduce

  1. pnpm init
  2. pnpm i -D typescript
  3. npx tsc --init
  4. npx zenstack init
  5. Copy paste the following models to your schema:
model Post {
    id Int @id @default(autoincrement())
    name String

    type String
    @@delegate(type)

    // full access by author
    @@allow('all', true)
}

model ConcretePost extends Post {
    age Int
}
  1. Run npx zenstack generate && npx prisma migrate dev --name "update"
  2. Create index.ts file at root and copy paste the following content:
import { PrismaClient as Prisma } from "@prisma/client";
import { enhance } from "@zenstackhq/runtime";

const prisma = new Prisma()
const db = enhance(prisma, {}, {
  kinds: ['delegate']
})

async function main() {
  const newPost = await db.concretePost.create({
    data: {
      id: 5,
      name: 'a name',
      age: 20
    }
  })
  const posts = await db.post.findMany({})
  console.log('posts', posts)
}

main()

Now you will see that typescript shouts on data saying that type field is missing.

Btw if you execute this code (You can run on terminal npx tsx index.ts) it will work just fine.

Environment (please complete the following information):

  "devDependencies": {
    "prisma": "^5.20.0",
    "typescript": "^5.6.2",
    "zenstack": "2.6.2"
  },
  "dependencies": {
    "@prisma/client": "^5.20.0",
    "@zenstackhq/runtime": "2.6.2"
  }
ymc9 commented

Description and expected behavior Typescript asks for delegation field when creating new polymorphic instance when providing id with a default of auto increment.

Screenshots image

Steps to reproduce

  1. pnpm init
  2. pnpm i -D typescript
  3. npx tsc --init
  4. npx zenstack init
  5. Copy paste the following models to your schema:
model Post {
    id Int @id @default(autoincrement())
    name String

    type String
    @@delegate(type)

    // full access by author
    @@allow('all', true)
}

model ConcretePost extends Post {
    age Int
}
  1. Run npx zenstack generate && npx prisma migrate dev --name "update"
  2. Create index.ts file at root and copy paste the following content:
import { PrismaClient as Prisma } from "@prisma/client";
import { enhance } from "@zenstackhq/runtime";

const prisma = new Prisma()
const db = enhance(prisma, {}, {
  kinds: ['delegate']
})

async function main() {
  const newPost = await db.concretePost.create({
    data: {
      id: 5,
      name: 'a name',
      age: 20
    }
  })
  const posts = await db.post.findMany({})
  console.log('posts', posts)
}

main()

Now you will see that typescript shouts on data saying that type field is missing.

Btw if you execute this code (You can run on terminal npx tsx index.ts) it will work just fine.

Environment (please complete the following information):

  "devDependencies": {
    "prisma": "^5.20.0",
    "typescript": "^5.6.2",
    "zenstack": "2.6.2"
  },
  "dependencies": {
    "@prisma/client": "^5.20.0",
    "@zenstackhq/runtime": "2.6.2"
  }

Hi @Tsimopak , thanks for the detailed repro steps. However I couldn't reproduce the issue following it.

I've shared my project here: https://github.com/ymc9/issue-1763

Could you help check what might be different from yours?

Hi @ymc9,

When I clone your repo, do pnpm i and then npx zenstack generate && npx prisma migrate dev --name "update" and then look into the index.ts file i see the same error.

I also tried to close and get again to VSCODE and issue persists:

Type '{ id: number; name: string; age: number; }' is not assignable to type '(Without<ConcretePostCreateInput, ConcretePostUncheckedCreateInput> & ConcretePostUncheckedCreateInput) | (Without<...> & ConcretePostCreateInput)'.
  Type '{ id: number; name: string; age: number; }' is not assignable to type 'Without<ConcretePostCreateInput, ConcretePostUncheckedCreateInput> & ConcretePostUncheckedCreateInput'.
    Property 'type' is missing in type '{ id: number; name: string; age: number; }' but required in type 'ConcretePostUncheckedCreateInput'.ts(2322)
index-fixed.d.ts(2559, 9): 'type' is declared here.
index-fixed.d.ts(2308, 9): The expected type comes from property 'data' which is declared here on type '{ select?: ConcretePostSelect<DefaultArgs> | null | undefined; data: (Without<ConcretePostCreateInput,