0
我試圖將在nodej上使用貓鼬的現有項目轉換爲打字稿。但是,我遇到了在一些回調函數中定義這種形狀的問題。 例如,我的用戶對象定義(重縮減到重點考慮的問題),如下所示:如何在打字稿回調函數中定義此變量的形狀
export class UserAPI{
userSchema: mongoose.Schema
constructor(){
this.userSchema = new mongoose.Schema({name: String,
email: {type:String, lowercase: true}
});
this.userSchema.virtual('password').set(function(password: string){
this._password = password;
}).get(function(){
return this._password;
});
}
}
上述(返回this._password)最後行導致打字稿錯誤。據我所知,獲取和設置回調函數的範圍由貓鼬設置。顯然,typescript不知道回調函數中這個對象的形狀,並導致編譯器錯誤。有人能幫助我理解如何克服這個錯誤嗎?