pgtyped icon indicating copy to clipboard operation
pgtyped copied to clipboard

Narrow parameter types to readonly

Open bradleyayers opened this issue 3 years ago • 4 comments

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.

bradleyayers avatar Mar 13 '22 09:03 bradleyayers

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

vercel[bot] avatar Mar 13 '22 09:03 vercel[bot]

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.

bradleyayers avatar Mar 13 '22 09:03 bradleyayers

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.

adelsz avatar Mar 16 '22 22:03 adelsz

@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.

danvk avatar Mar 22 '22 19:03 danvk