Hey guys looking for some typescript help. I have ...
# orm-help
d
Hey guys looking for some typescript help. I have a dynamic upsert function like so
Copy code
ctx.prisma[`${var)Model`].upsert(....
Typescript automatically creates a union with the variable possiblities but as these models have different fields I get an error like
Copy code
Each member of the union type '(<T extends  ....  ..... => CheckSelect<...>)' has signatures, but none of those signatures are compatible with each other.
Is there anyway to fix this problem?
m
That's right, if you want to access a common member in an union (in this case, the
upsert
), then TypeScript (for safety) will only allow you to access the intersection of those. This is to prevent you accessing things that could potentially not exist or be incompatible across members of the union. In this case, the methods signatures are incompatible.
You have two choices: • You discriminate your union
Copy code
function autoUpsert1(model: 'user' | 'link') {
    const prisma = new PrismaClient()

    if (model === 'link') {
      return prisma[model].upsert({})
    }

    if (model === 'user') {
      return prisma[model].upsert({})
    }
}
• You go into unsafe mode with any
Copy code
function autoUpsert2(model: 'user' | 'link') {
  const prisma = new PrismaClient()

  return (prisma[model] as any).upsert({})
}
Hope this helps šŸ™‚
šŸ‘ 1