Hi I am struggling getting Typescript to work with my current setup. For reference this is cut down version of my prisma Model.
model Invoice {
id String @id @default(uuid())
invoiceNumber String?
clientId String?
client Client? @relation(fields: [clientId], references: [id])
companySettings CompanySettings @relation(fields: [companyId], references: [id])
companyId String
lineItems InvoiceLineItem[]
userId String
user User @relation(fields: [userId], references: [id])
}
model InvoiceLineItem {
id String @id @default(uuid())
itemName String?
invoice Invoice @relation(fields: [invoiceId], references: [id])
invoiceId String
}
model Client {
id String @id @default(uuid())
name String @unique @db.VarChar(256)
user User @relation(fields: [userId], references: [id])
userId String @db.VarChar(256)
invoices Invoice[]
}
So a Client
can have many invoices. An Invoice
can have many lineItems. So I tried replicating this as an interface
export interface InvoiceLineItem {
id: string;
itemName?: string | null;
invoiceId: string;
}
export interface Invoice {
id: string;
invoiceNumber?: string | null;
clientId?: string | null;
companyId: string;
lineItems?: InvoiceLineItem[];
userId: string;
}
export interface Client {
id?: string;
name: string;
userId: string;
invoices?: Invoice[];
}
So I want to retrieve all clients so I have the following
export const fetchClients = async (): Promise<Client[] | null> => {
try {
const clientsRes = await GetClients();
if (clientsRes.success && clientsRes.clients) {
return clientsRes.clients;
} else {
return null;
}
} catch (error) {
console.error(error);
return null;
}
};
Whats returned is something like this
[
{
id: '70414b0a-a9f2-400f-8c88-e57cb9ca2050',
name: 'Test client',
creationDate: 2024-06-03T14:40:00.181Z,
userId: 'asdasd',
invoices: []
}
]
Now at this point its one client, with no invoices, hence no lineItems. But I would have thought an empty lineItems array would be included in the response as my action is this
const clients = await db.client.findMany({
where: {
userId: user.id ? { equals: user.id } : undefined,
},
include: {
invoices: {
include: {
lineItems: true,
},
},
},
});
And I think thats where fetchClients is complaining, more specifically return clientsRes.clients;
I get
Type '({ invoices: ({ lineItems: { id: string; itemName: string | null; invoiceId: string; }[]; } & { ...; })[]; } & { ...; })[]' is not assignable to type 'Client[]'.
Types of property 'invoices' are incompatible.
Type '({ lineItems: { id: string; itemName: string | null; invoiceId: string; }[]; } & { ...; })[]' is not assignable to type 'Invoice[]'.
Am I missing something here? Is it complaining due to not having lineItems in the response? Sorry, first time using Typescript to trying to fully understand things.
Thanks