© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
Drizzle TeamDT
Drizzle Team•15mo ago•
26 replies
NeonCop

Types on conditional joins

Hey everyone!
I'm struggling with maintaining type safety when adding conditional joins in Drizzle.
I have a function that builds a query with conditional joins based on parameters. Depending on the parameters, I might need to join different tables. However, TypeScript isn't correctly inferring the return type, and I have to cast it, which defeats the purpose of type safety.
Here's a simplified version of my code:

type JoinOptions = Partial<Record<'withFoo' | 'withBar', true>>;

type BaseFields = ReturnType<typeof getTableColumnAliases<typeof baseTable>>;
type FooFields = ReturnType<typeof getTableColumnAliases<typeof fooTable>>;
type BarFields = ReturnType<typeof getTableColumnAliases<typeof barTable>>;

type MyQuerySelection<T extends JoinOptions> = {
base: BaseFields;
} & (T['withFoo'] extends true ? { foo: FooFields } : {}) &
(T['withBar'] extends true ? { bar: BarFields } : {});

export function buildMyQuery<T extends JoinOptions>({
params,
joins = {} as T,
}: {
params: MyQueryParams;
joins?: T;
}) {
const selectClauses = {
base: getTableColumnAliases(baseTable),
...(joins.withFoo && { foo: getTableColumnAliases(fooTable) }),
...(joins.withBar && { bar: getTableColumnAliases(barTable) }),
};

let query = db.select(selectClauses).from(baseTable).$dynamic();

if (joins.withFoo) {
query = withFoo({ query, params });
}

if (joins.withBar) {
query = withBar({ query, params });
}

return query;
}

But even with conditional types, TypeScript doesn't infer the return type based on joins. I have to cast the return type, which isn't ideal.
Here's how I'm using the function:

const query = buildMyQuery({
params: myParams,
joins: { withFoo: true },
});

const results = await query.execute();

results.forEach((row) => {
console.log(row.foo?.someField); // Error: Property 'foo' does not exist
});

Has anyone faced this issue or have suggestions?
Thanks in advance!
Drizzle TeamJoin
The official Discord for all Drizzle related projects, such as Drizzle ORM, Drizzle Kit, Drizzle Studio and more!
11,879Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

Conditional returning, returning wrong types
Drizzle TeamDTDrizzle Team / help
2y ago
How to do conditional joins with the query builder?
Drizzle TeamDTDrizzle Team / help
3y ago
Nested joins / where on relation
Drizzle TeamDTDrizzle Team / help
3y ago
Lateral joins
Drizzle TeamDTDrizzle Team / help
17mo ago