How do I define type of returned object based on selected fields?

I have a function that fetches a client and optionally only selects some of the client props:

// Example implementation of clientSelect
const clientSelect = {
   a: true,
   b: true,
   c: true
}

export const getClientById = async (
  clientId: number,
  select?: Prisma.ClientSelect
) => {
  return await prisma.client.findFirst({
    select: select || clientSelect,
    where: {
      id: clientId,
    },
  });
};


I would like to be able to call
getClientById
and have the return type correctly inferred based on what is passed for
select
. For example:

const myClient = getClientById(333, { a: true })

// I want TypeScript to understand that myClient contains a and doesn't contain b and c


What is the correct way to achieve this?
Was this page helpful?