© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
Drizzle TeamDT
Drizzle Team•12mo ago
uri

Best practices for inferring types in queries?

I'm refactoring my app and I'm trying to keep my schema as the single source for my data types, at least for my model data.

Inferring types has worked wonders for inserting and making basic queries.

However, for more complex queries with nested data I've tried to make intersection types matching the structure of the query result, but ts doesn't like that (the structure might not be identical), and explicit casting doesn't seem to work either.

For instance, having something like this:
 const users= await db.query.user.findMany({
        with: {
            posts: {
                with: {
                    post: true,
                },
                columns: {
                    userId: false,
                    postId: false,
                },
            },
            postGroup: true,
            images: true,
        },
    });
 const users= await db.query.user.findMany({
        with: {
            posts: {
                with: {
                    post: true,
                },
                columns: {
                    userId: false,
                    postId: false,
                },
            },
            postGroup: true,
            images: true,
        },
    });

I tried to create the type (didn't work):
export type User = BaseUser & {
    posts: Post[],
    postGroup: PostGroup
    images: Images[],
}
export type User = BaseUser & {
    posts: Post[],
    postGroup: PostGroup
    images: Images[],
}


Another idea I have is that I could infer the type from the users object, but in that case that would imply having my user queries centralized in some place so I could export their types to the multiple places needed.

I'm trying to find a way to get proper typing while avoiding to duplicates or having to manually write types them throughout my app.

What do you think it could be an elegant solution? Thank you!
Drizzle TeamJoin
The official Discord for all Drizzle related projects, such as Drizzle ORM, Drizzle Kit, Drizzle Studio and more!
11,879Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Best practices for complex queries
Drizzle TeamDTDrizzle Team / help
11mo ago
Inferring types for nested objects
Drizzle TeamDTDrizzle Team / help
2y ago
Inferring Types from Schema?
Drizzle TeamDTDrizzle Team / help
13mo ago
Querying Best Practices?
Drizzle TeamDTDrizzle Team / help
3y ago