The `UserType` interface is then read in many places
// Entityconstuser=newUser({ name:'John' }); // TS error as "email" is not provided// Queriesconstquery=User.query();query.filter('age','<','wrong string'); // TS error as "age" should be a numberquery.filter('wrongProperty','some value'); // TS error as "wrongProperty" does not exist in User// We can also specify the format of the query response ("JSON" is the default and thus optional)constquery=User.query<'ENTITY'>(); // Set the response format to "ENTITY"query.run({ format:'ENTITY' }).then(result => {const { entities } = result;const [user] = entities;// "user" is an Entity instance with its methods and propertiesuser.save();constname=user.name; // string});// Shortcut queries (`list()` and `findAround()`) response// are automatically typed from the `format` option providedUser.list({ format:'ENTITY' }).populate().then((result) => {const { entities } = result;const [user] = entities;constage=user.age; // numberuser.save(); // method from the entity instance});
Non explicit schemas
It you want to allow other properties apart from those declared (see explicitOnly option in the Schema options), this is how you would create your Model:
typeUserType= { userName:string; email:string; age?:number; // optional tags?:string[]; // optional birthday?:Date; // optional} & {[propName:string]:any}; // Allow any other properties// Schema with "explicitOnly" set to "false"constschema=newSchema<UserType>({ userName: { type: String }, email: { type: String }, age: { type: Number, optional:true }, tags: { type: Array, optional:true }, birthday: { type: Date, optional:true }}, { explicitOnly:false }); // explicitOnly set to "false"constUser=gstore.model('User', schema);