Replies: 1 comment
-
Sorry for the very late reply, we missed this discussion when it came in. Mongoose doesn't provide a utility to convert ObjectId and Date properties, but I think we should. Converting ObjectIds would look something like the following. Ideally we would wrap FlattenObjectIds with FlattenDates and FlattenMaps into one ToJSON helper. type FlattenObjectIds<T> = T extends TreatAsPrimitives ? T : T extends Record<string, any> ? {
[K in keyof T]: T[K] extends ObjectId
? string
: T[K] extends (ObjectId | null | undefined)
? string | null | undefined
: T[K] extends Types.DocumentArray<infer ItemType>
? Types.DocumentArray<FlattenObjectIds<ItemType>>
: T[K] extends Types.Subdocument<unknown, unknown, infer SubdocType>
? HydratedSingleSubdocument<FlattenObjectIds<SubdocType>>
: FlattenObjectIds<T[K]>;
} : T; In the future, we would ideally instead infer the JSON type from the schema rather than converting after the fact because of issues like #13523. So that would mean we would have to infer the following types from the schema: 1) raw doc type, 2) hydrated doc type, 3) JSON serialized doc type |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hi!
Given the example on the mongoose typescript documentation:
And i then send a
user
over a REST-API (where it gets JSON-stringified), the resulting object in the frontend ends up being like this:How can i infer the type coming from
IUser
in the frontend, whereObjectId
s andDate
s are now strings?Bonus Question: How can i infer the stringified
HydratedDocument<IUser>
so i have the virtuals likeid
in there too?Beta Was this translation helpful? Give feedback.
All reactions