mongoose - Reducing an interface in TypeScript -


i have mongoose model interface declaring such:

export interface iuserdocument extends mongoose.document {   surname: string;   lastname: string; }  export interface iusermodel extends mongoose.model<iuserdocument> {   mystaticmethodgoeshere(signature: string): void; } 

and corresponding mongoose schema:

const userschema = new mongoose.schema({   surname: {type: string, required: true},    lastname: {type: string, required: true}, }) 

now have define how user structured 2 times (mongoose schema , typescript interface), isn't dry , inconvenient. unfortunately, if want use mongoose's lean method, have define yet interface:

export interface ileanuserdocument {   _id: mongoose.types.objectid;   __v: number;   surname: string;   lastname: string; } 

this inconvenient , error prone. there way lean interface extend (i don't know verb use here) iuserdocument interface remove stuff it?

extends not way of adding properties interface. believe can work type intersection achieve you're looking for. note in example below names guesses appropriate, feel free choose whatever like.

you can define interface

export interface iuserproperties {   surname: string;   lastname: string; }  export type iuserdocument = mongoose.document & iuserproperties; 

and later on

interface ileanuserbase {   _id: mongoose.types.objectid;   __v: number; }  export type ileanuserdocument = ileanuserbase & iuserproperties; 

changes iuserproperties reflected in both iusermodel , iuserdocument


Comments

Popular posts from this blog

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -

mongodb - How to keep track of users making Stripe Payments -