question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Usage of Readonly types instead of mutable in TS typings

See original GitHub issue

Currently 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:open
  • Created 4 years ago
  • Reactions:7
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

14reactions
Syttencommented, Nov 23, 2020

Agreed but what about setting Enumerate to

export type Enumerable<T> = T | Array<T> | ReadonlyArray<T>

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

0reactions
flevi29commented, Aug 9, 2022

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 Enumerable or not, and if it is just simplify it to a dead simple array T[]. I really have a hard time understanding generics, but I got a working solution somehow:

import { Prisma } from '@prisma/client';

type Unpacked<T> = T extends (infer U)[] ? U : never;

type PrismaMappedSomething = {
  [k in keyof Prisma.PrismaSomething]: Unpacked<
    Prisma.PrismaSomething[k]
  > extends never
    ? Prisma.PrismaSomething[k]
    : Unpacked<Prisma.PrismaSomething[k]>[];
};

It can probably be simplified and made more elegant but any attempt at it failed for me.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Usage of Readonly types instead of mutable in TS typings
If I understand correctly, currently we use only one generic for all input types: export declare type Enumerable<T> = T | Array<T>; ,...
Read more >
Read-Only Array and Tuple Types in TypeScript - Marius Schulz
We can now use the readonly modifier to create read-only array types ... that it returns an array of the same type instead...
Read more >
How can I ensure that a readonly property is NOT assignable ...
You can achieve "nominal typing", i.e. making similar types incompatible despite sharing the same structure, by using a technique called ...
Read more >
Documentation - TypeScript 3.4
Despite its appearance, the readonly type modifier can only be used for syntax on array types and tuple types. It is not a...
Read more >
Change a readonly property to mutable in TypeScript
You can use mapping modifiers to change a readonly property to mutable in TypeScript, e.g. `-readonly [Key in keyof Type]: Type[Key]`.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found