pgtyped
pgtyped copied to clipboard
Narrow parameter types to readonly
This allows consumers to pass in readonly arrays.
I had a problem where in my application I was using readonly types for added safety, but then hit a problem where I couldn't pass those directly into Pgtyped.
This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployment, click below or on the icon next to each commit.
🔍 Inspect: https://vercel.com/adels/pgtyped/B7tpYHfTS5iPHpNwRfDFEFimnECQ
✅ Preview: https://pgtyped-git-fork-bradleyayers-bradleyayers-readonly-types-adels.vercel.app
This is too naive actually, as it affects the result types too rather than just parameter types. I'll need to re-think the approach.
This is too naive actually, as it affects the result types too rather than just parameter types. I'll need to re-think the approach.
Yes, maybe it can be added similarly to how nullability is being handled dynamically. The Json type would be a bit more tricky, but it should be probably be fine to have a separate JsonParam type that is read-only.
@bradleyayers do you want to add the readonly / Readonly annotation on the method to run the query? For example here:
https://github.com/adelsz/pgtyped/blob/55d131c9b1bdcc2fcfc0ffec2229c6ed4bdfc644/packages/query/src/tag.ts#L12-L15
params would become params: Readonly<TTypePair['params']>, or even params: DeepReadonly<TTypePair['params']> (see ts-essentials for DeepReadonly). This would accomplish your goal w/o affecting any of the named data types.