Usage of Readonly types instead of mutable in TS typings
See original GitHub issueCurrently all arrays in input positions use Array type and it leads to a lot of issues, such as incompatibility with Facebook DataLoader library. DataLoader expect keys to be a ReadonlyArray, while Prisma expects {where: { id: { in: keys } }} to accept mutable Array. I think it’s safe to change types of arrays in input positions to ReadonlyArray as well (i believe Prisma doesn’t mutate them under the hood, so it’s safe to do that), while keeping types in output positions as Array (we shouldn’t enforce users to keep result array immutable, so we can’t use ReadonlyArray here).
If I understand correctly, currently we use only one generic for all input types: export declare type Enumerable<T> = T | Array<T>;, so changing it to export declare type Enumerable<T> = T | ReadonlyArray<T>; should be enough.
It would be nice to have immutable types for objects as well, like
export declare type UserCreateInput = Readonly<{
id?: string | null;
name: string;
}>;
instead of
export declare type UserCreateInput = {
id?: string | null;
name: string;
};
because of the same reasons as described above. If Prisma doesn’t mutate this object, it’s better to mark it as immutable, so user will be able to pass both mutable/immutable objects, not only mutable ones like right now
Issue Analytics
- State:
- Created 4 years ago
- Reactions:7
- Comments:8 (5 by maintainers)

Top Related StackOverflow Question
Agreed but what about setting Enumerate to
That would solve my issue with dataloaders. Dataloaders use a ReadonlyArray in the load method so currently I have to cast it to an array. https://github.com/graphql/dataloader/blob/master/src/index.d.ts#L74
This might not belong here but I leave it here in the hopes that someone might find it useful. I just need to know whether a property can be
Enumerableor not, and if it is just simplify it to a dead simple arrayT[]. I really have a hard time understanding generics, but I got a working solution somehow:It can probably be simplified and made more elegant but any attempt at it failed for me.