Typegoose & MongoDB

Hello fellow humans,

I was working on a fullstack app with NestJS for the backend. And my database is mongodb. I also discovered an awesome plugin which keeps the typescript interfaces up to date with the mongodb schema. It is called ‘typegoose’. (https://www.npmjs.com/package/typegoose).

So there is a pre hook which is called when a specific action is going to happen. And this hook lives in my base class. So I used a generic type for the hook, but I get an error “Cannot find T”.

Here is my code:

@pre<T>('findOneAndUpdate', function(next) {
  this._updated.updatedAt = new Date(Date.now());
  next();
})
export class BaseModel<T> extends Typegoose {
  @prop({ default: Date.now() })
  createAt?: Date;

  @prop({ default: Date.now() })
  updatedAt?: Date;

  id?: string;
}

The T in @pre is making the trouble. You got any ideas why?

Ever get an answer to this? I just ran into the same problem.

if you have questions, please feel free to open an issue at https://github.com/szokodiakos/typegoose/issues