Mongoose “property x does not exist on type y” error - still works
在Mongoose中,尝试使用点表示法(
能够在猫鼬中正确使用点符号的正确方法是什么?
背景:
location.model.ts
1 2 3 4 5 6 7 8 9 | import mongoose = require('mongoose'); export const LocationSchema = new mongoose.Schema({ name: String, lowTemp: Number, highTemp: Number, }); export const Location = mongoose.model('Location', LocationSchema); |
data.util.ts
1 2 3 4 5 6 7 8 9 10 | import { Location } from '../models/location.model'; function temperatureModel(location: Location): number { const highTemp = location.highTemp; const lowTemp = location['lowTemp']; // Do the math... return something; } |
构建以上内容会在
1 2 3 4 5 6 7 8 9 10 11 12 13 | export interface Location extends mongoose.Document { name: string, lowTemp: number, highTemp: number, } export const Location = mongoose.model<Location>('Location', LocationSchema); // Usage function temperatureModel(location: Location): number { const highTemp = location.highTemp; // Works const lowTemp = location.lowTemp; // Works } |