TypeScript error when using select argument type
Hi,
The following code snippet is working fine,
and TypeScript started to complaint when I tried to extract the query options out to a
options
variable like this,
The error looks like this,
Property 'products_orders_order_product_idToproducts' does not exist on type { ... }
Do you have any idea how to resolve this?3 Replies
Hi @TJ
Can you try this code
That is working but, why?
When you use
Prisma.ordersFindManyArgs
to describe your options
in TypeScript, it tells TypeScript, This is a valid input for findMany.
but the downside is that TypeScript forgets the exact details about the fields you're selecting (like which ones you want). To fix this, we use a technique called as const satisfies
.
1. as const
tells TypeScript to treat your options exactly as you've written them, with all the details about selected fields.
2. satisfies Prisma.ordersFindManyArgs
double-checks that your options
follow the rules for findMany
.
This keeps the exact information about the fields you selected, so TypeScript won’t complain when you use them later in your code.
You can also look at this previous Github discussion.GitHub
Typescript throw error when abstracting nested select · prisma pris...
Bug description It is very easy to reproduce this, I don't know if it work as intended or not. So I have this simple prisma schema, basically just a 3 relation table, A have B children, B have ...