P
Prisma3mo ago
TJ

TypeScript error when using select argument type

Hi, The following code snippet is working fine,
const results = await prisma.orders.findMany({
select: {
...,
products_orders_order_product_idToproducts: {
select: {
orders: {
select: {
clients: {
select: {
name: true,
},
},
},
},
},
},
},
});
return results.map((row) => ({
...
name:
row.products_orders_order_product_idToproducts?.orders
.clients?.name, // No TypeScript error
}));
const results = await prisma.orders.findMany({
select: {
...,
products_orders_order_product_idToproducts: {
select: {
orders: {
select: {
clients: {
select: {
name: true,
},
},
},
},
},
},
},
});
return results.map((row) => ({
...
name:
row.products_orders_order_product_idToproducts?.orders
.clients?.name, // No TypeScript error
}));
and TypeScript started to complaint when I tried to extract the query options out to a options variable like this,
const options: Prisma.ordersFindManyArgs = {
select: {
...,
products_orders_order_product_idToproducts: {
select: {
orders: {
select: {
clients: {
select: {
name: true,
},
},
},
},
},
},
},
});

const results = await prisma.orders.findMany(options)
return results.map((row) => ({
...
name:
row.products_orders_order_product_idToproducts?.orders
.clients?.name, // TypeScript error here
}));
const options: Prisma.ordersFindManyArgs = {
select: {
...,
products_orders_order_product_idToproducts: {
select: {
orders: {
select: {
clients: {
select: {
name: true,
},
},
},
},
},
},
},
});

const results = await prisma.orders.findMany(options)
return results.map((row) => ({
...
name:
row.products_orders_order_product_idToproducts?.orders
.clients?.name, // TypeScript error here
}));
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
RaphaelEtim
RaphaelEtim3mo ago
Hi @TJ Can you try this code
const options = {
select: {
// ... your other selections
products_orders_order_product_idToproducts: {
select: {
orders: {
select: {
clients: {
select: {
name: true,
},
},
},
},
},
},
},
} as const satisfies Prisma.ordersFindManyArgs;

const results = await prisma.orders.findMany(options);
return results.map((row) => ({
// ... your other mappings
name: row.products_orders_order_product_idToproducts?.orders.clients?.name,
}));
const options = {
select: {
// ... your other selections
products_orders_order_product_idToproducts: {
select: {
orders: {
select: {
clients: {
select: {
name: true,
},
},
},
},
},
},
},
} as const satisfies Prisma.ordersFindManyArgs;

const results = await prisma.orders.findMany(options);
return results.map((row) => ({
// ... your other mappings
name: row.products_orders_order_product_idToproducts?.orders.clients?.name,
}));
TJ
TJOP3mo ago
That is working but, why?
RaphaelEtim
RaphaelEtim3mo ago
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 ...

Did you find this page helpful?