Generic parameter in function for includes
Is this not supposed to work like this? I get some cryptic error messages:
3 Replies
You've gone full bot mode! Let me fetch that AI answer for you ASAP.
Your approach is close, but Prisma's type inference for include/select parameters in wrapper functions or with generics is known to be problematic. When you use a generic parameter like <T extends true> for the include value, Prisma's types don't always propagate as you might expect. This is a well-documented pain point in the community.
The main issue is that Prisma expects a static value (true or an object) for includes, and when you use a generic, TypeScript can't always infer the correct return type. As a result, you may get cryptic or incorrect type errors, or the return type may not reflect the actual shape of the data returned by Prisma. This is discussed in detail in several issues and discussions:
- If you use a boolean variable for an include, Prisma will not infer the type as optional, even though at runtime the field may be missing. For example, if you pass a boolean variable, the related field is always typed as present, not as possibly undefined or missing. This is a known limitation (source).
- To get correct type inference, you typically need to use a generic that extends the full include type, and pass that generic to the Prisma call, like so:
This way, the return type is dynamic based on the include parameter (source).
- If you want to conditionally include a relation based on a boolean, you may need to write a custom type or use a union/intersection type trick, but this is verbose and not natively supported by Prisma's types (source).
In summary: Your code will not work as expected for type inference. You n
eed to use a generic that extends the full include type and pass it to the Prisma call for correct type inference. Using a boolean generic for a single include does not propagate the type correctly in Prisma's current type system.
š¤ Alright, thanks for letting us know that there's still some help needed. A team member will reply to you shortly.
In the meantime, please let us know any additional info, questions, or which parts of the answer may need clarification.
could someone explain that to me, pretty please?
I also tried using T for the full include objects and check that it extends the VoiceChannelSessionInclude, that didnt throw this error
I guess the AI kinda explained it to me, assuming it is correct what it says, that a generic parameter WITHIN an object does not propagate properly.
I tried earlier to have a full generic object, and that worked fine. ill just go with that