ossuboi
ossuboi
DTDrizzle Team
Created by ossuboi on 10/2/2024 in #help
Typing issue - findFirst/findMany `with` parameter with a dynamic value
I am having an issue using findFirst and findMany where including a dynamic value for the with parameter breaks the types for the output. Here's a simplified example of what I have going on:
export const findWorkById = async (id: number, scope: "basic" | "extended") => {
const work = await db.query.works.findFirst({
where: eq(works.id, id),
with: scope === "extended" ? { workDescription: true, workContact: true } : undefined,
});

return work;
};
export const findWorkById = async (id: number, scope: "basic" | "extended") => {
const work = await db.query.works.findFirst({
where: eq(works.id, id),
with: scope === "extended" ? { workDescription: true, workContact: true } : undefined,
});

return work;
};
When calling the findWorkById method with the scope being extended I would expect the type of the returned value to be:
const work: {
id: number;
companyId: number;
createdAt: Date;
updatedAt: Date;
workDescription: {
...;
};
workContact: {
...;
};
} | undefined
const work: {
id: number;
companyId: number;
createdAt: Date;
updatedAt: Date;
workDescription: {
...;
};
workContact: {
...;
};
} | undefined
But whatever I pass as the scope value the return type is always the 'base' type for the work schema which is:
const work: {
id: number;
companyId: number;
createdAt: Date;
updatedAt: Date;
} | undefined
const work: {
id: number;
companyId: number;
createdAt: Date;
updatedAt: Date;
} | undefined
When I provide the { workDescription: true, workContact: true } directly to the with parameter, without the ternary, the the returned object has the correct type. Has anyone faced this issue and have a good solution? Do I just have morning-brain and am missing something silly? Any help is appreciated!
3 replies