I’m using Typegoose to define Mongoose models with TypeScript. According to the Typegoose documentation, type suggestions are provided when using new Model({}), but they are not strictly enforced.
new KittenModel({}) will have type suggestions, but
they are not enforcedSince around mongoose 6.0.0, new Model() and Model.create() have
types, but they are not enforced (as in they will be suggested, but
will not error if not present / other unknown properties are present).
Typegoose FAQ – Why does new Model({}) not have types?
class KittenClass {
@prop()
public name?: string;
}
const KittenModel = getModelForClass(KittenClass);
let document = await KittenModel.create({ name: 'Kitty' });
So I won’t get any error if wrong properties are inserted. How to handle this best to preserve typesafety?
await KittenModel.create(
{
name: 'Kitty',
test: 'test' // <-- this will not throw an ts-error
}
);