Replies: 1 comment
|
On recent Drizzle ( const post = await db.query.posts.findFirst();
// type: typeof posts.$inferSelect | undefined
post.title; // TS: 'post' is possibly 'undefined'
if (post) {
post.title; // safe
}If you're seeing pnpm update drizzle-ormTemporary cast if you can't upgrade yet: const post = await db.query.posts.findFirst() as typeof posts.$inferSelect | undefined;Or an explicit signature on your repo functions so callers see the right type regardless of the underlying version: async function getPost(id: number): Promise<typeof posts.$inferSelect | undefined> {
return db.query.posts.findFirst({ where: eq(posts.id, id) });
}Side note: when a |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
I picked drizzle orm considering it's strong type safety and just realized
db.query.<tbl>.findFirstdoes not return an optional value while at runtime I can get undefined without error out. Is there a way to fix typing or do I have to manually type every repository function I have to includePromise<ExpType | undefined>?All reactions