You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
import{Schema,Types}from'mongoose';// 1. Create an interface representing a document in MongoDB.interfaceIUser{name: string;email: string;// Use `Types.ObjectId` in document interface...organization: Types.ObjectId;}// 2. Create a Schema corresponding to the document interface.constuserSchema=newSchema<IUser>({name: {type: String,required: true},email: {type: String,required: true},// And `Schema.Types.ObjectId` in the schema definition.organization: {type: Schema.Types.ObjectId,ref: 'Organization'}});
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, where ObjectIds and Dates are now strings?
Bonus Question: How can i infer the stringified HydratedDocument<IUser> so i have the virtuals like id in there too?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
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