plexinc / papr

MongoDB TypeScript-aware Models
https://plexinc.github.io/papr/
MIT License
426 stars 22 forks source link

feat: TS types for updates with readonly array values #829

Open duncanbeevers opened 1 month ago

duncanbeevers commented 1 month ago

🐞 Permit readonly arrays

Related issue: https://github.com/plexinc/papr/issues/828

Test case demonstrating TS type error.

image
duncanbeevers commented 1 month ago

@avaly We got a different set of failures on media-providers with these changes. However, I went ahead and linked my locally-built papr into media-providers and made one simple adjustment. This change does make this a breaking change, since PropertyType is part of the public contract.

diff --git a/@types/mongodb.d.ts b/@types/mongodb.d.ts
index c9004704d4..2ce4be1716 100644
--- a/@types/mongodb.d.ts
+++ b/@types/mongodb.d.ts
@@ -36,6 +36,6 @@ declare global {
   // This type is extracted from `papr.PaprMatchKeysAndValues`, because that type is too complex for our usage here,
   // and it would introduce more than 3 minutes on the `tsc` check times.
   export type MongoAttributes<TSchema> = {
-    [Property in mongodb.Join<papr.NestedPaths<TSchema, []>, '.'>]?: papr.PropertyType<TSchema, Property>;
+    [Property in mongodb.Join<papr.NestedPaths<TSchema, []>, '.'>]?: Mutable<papr.PropertyType<TSchema, Property>>;
   };
 }
CarlosLozanoHealthCaters commented 3 weeks ago

@avaly I've tested it in our internal repos and it is compiling without errors.

Should we adapt other methods like insert to support readonly array too?

avaly commented 3 weeks ago

Thanks for testing @CarlosLozanoHealthCaters! Yeah, we plan to extend support to all methods that accept user input.