类型“ typeof Schema”上不存在属性“ virtual”

wen*_*mva 5 mongoose node.js typescript

我正在尝试将mongoose模式的change_id更改为“ id”,如以下所示:MongoDB:输出为“ id”而不是“ _id”

复制ID字段。

Schema.virtual('id').get(function(){
    return this._id.toHexString();
});

// Ensure virtual fields are serialised.
Schema.set('toJSON', {
    virtuals: true
});
Run Code Online (Sandbox Code Playgroud)

我正在使用打字稿,并且Schema似乎既没有“虚拟”方法也没有“设置”方法,并且关键字“ this”在此上下文中也不受约束。谁知道他们的打字稿对等物?

Web*_*ber 3

这似乎适用于继承

import { Schema } from 'mongoose';

class BaseSchema extends Schema {
  constructor(args) {
    super();

    this.add(args);

    this.virtual('id').get(function(this: any) {
      return this._id.toHexString();
    });

    this.set('toObject', {
      virtuals: true,
    });

    this.set('toJSON', {
      virtuals: true,
    });
  }
}
Run Code Online (Sandbox Code Playgroud)