Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Tsimopak opened this issue Oct 8, 2024 · 2 comments
Milestone

Comments

@Tsimopak
Copy link

Tsimopak commented Oct 8, 2024

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"
  }
@Tsimopak Tsimopak changed the title Typescript asks for delegation field when creating new polymorphic instance when providing id with a default of auto increment Typescript asks for delegation field when creating new polymorphic instance and providing id with a default of auto increment Oct 8, 2024
@Tsimopak Tsimopak changed the title Typescript asks for delegation field when creating new polymorphic instance and providing id with a default of auto increment [BUG] Typescript asks for delegation field when creating new polymorphic instance and providing id with a default of auto increment Oct 8, 2024
@ymc9 ymc9 added this to the v2.7.0 milestone Oct 15, 2024
@ymc9
Copy link
Member

ymc9 commented Oct 15, 2024

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?

@ymc9 ymc9 modified the milestones: v2.7.0, v2.8.0 Oct 17, 2024
@Tsimopak
Copy link
Author

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,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants