I'm using mongoose with typescript and today I stepped into some confusion with Model and Document.
Here is definition of my subdocument.
export interface IUserUnitResource {
serialNumber: string;
role: string;
name?: string;
alarms: IUserAlarmResource[];
}
export interface IUserUnitEntity extends IUserUnitResource, mongoose.Document {
alarms: mongoose.Types.DocumentArray<IUserAlarmEntity>;
}
export const userUnitSchema = new mongoose.Schema(
{
serialNumber: { type: String, required: true, index: true },
role: { type: String, default: 'user' },
name: String,
alarms: { type: [userAlarmSchema], default: [] },
},
{ _id: false }
);
In mongoose documentation they say:
An instance of a model is called a document.
So I assumed that i can call ownerDocument method on that subdocument. But typescript gives me an error:
Property 'ownerDocument' does not exist on type 'IUserUnitEntity'.
But if i try to call that method using any i get desired document.
(uu as any).ownerDocument()
Do I have the typings wrong, or it's a bug?