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
The reason to declare it as a variable is to guard against missing properties (i.e. if I add a new field to the User it will be giving me a compilation error even if a field is undefined).
Let me illustrate:
import{Document,Schema,SchemaDefinitionProperty}from"mongoose";typeUserType="admin"|"user";interfaceUser{_id: string;name: string;email: string;type: UserType;}interfaceUserDocumentextendsDocument<string>,User{_id: string;}/** * The reason to declare it as a field is to guard * against missing properties - is someone adds field to User * it will be required here. **/constfields: Record<Exclude<keyofUserDocument,keyofDocument>|"_id",SchemaDefinitionProperty>={_id: {type: String,default: ()=>"user"+Date.now()// FAKE generator for simplicity},name: String,email: {type: String,index: true,unique: true},type: String};/** * ERROR: Argument of type ... is not assignable to parameter of type * - Type 'string' is not assignable to type 'SchemaDefinitionProperty<UserType> | undefined' */newSchema<UserDocument>(fields);
I have a type error with the last statement, i.e. new Schema<UserDocument>(fields); generates an error: "...Type 'string' is not assignable to type 'SchemaDefinitionProperty | undefined'"
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
-
The reason to declare it as a variable is to guard against missing properties (i.e. if I add a new field to the
User
it will be giving me a compilation error even if a field is undefined).Let me illustrate:
I have a type error with the last statement, i.e.
new Schema<UserDocument>(fields);
generates an error: "...Type 'string' is not assignable to type 'SchemaDefinitionProperty | undefined'"What would be the proper type of
fields
?Beta Was this translation helpful? Give feedback.
All reactions