让 linter 将类型 "Ref <T>" 识别为 "T" 而不是 "ObjectId"
Have the linter recognize the type "Ref <T>" as "T" instead of "ObjectId"
当我 class 如下所示时:
export class Application{
@prop({ required: [true, 'app name is required'], unique: true })
name!: string;
@prop({ required: [true, 'component is required'], ref: Component})
component!: Ref<Component>;
}
并且假设 'Component' class 有一个 'name' 属性,我不能这样做:
let app: Application
const appName = 'appTest';
app = await (await this.findOne({ name: appName })).populate('component').execPopulate();
console.log(app.component.name);
因为它给我以下错误:
Property 'name' does not exist on type 'Ref<Component, ObjectId>'.
Property 'name' does not exist on type 'ObjectId'
有没有办法让 linter 将类型视为 T(来自 Ref<T>
)而不是 ObjectId?
目前,对我来说效果很好的是使用类型保护,特别是来自 Typegoose 的 isDocument and isDocumentArray。
应该是这样的:
let app: Application
const appName = 'appTest';
app = await (await this.findOne({ name: appName })).populate('component').execPopulate();
if (isDocument(app.component)) {
console.log(app.component.name);
}
当我 class 如下所示时:
export class Application{
@prop({ required: [true, 'app name is required'], unique: true })
name!: string;
@prop({ required: [true, 'component is required'], ref: Component})
component!: Ref<Component>;
}
并且假设 'Component' class 有一个 'name' 属性,我不能这样做:
let app: Application
const appName = 'appTest';
app = await (await this.findOne({ name: appName })).populate('component').execPopulate();
console.log(app.component.name);
因为它给我以下错误:
Property 'name' does not exist on type 'Ref<Component, ObjectId>'.
Property 'name' does not exist on type 'ObjectId'
有没有办法让 linter 将类型视为 T(来自 Ref<T>
)而不是 ObjectId?
目前,对我来说效果很好的是使用类型保护,特别是来自 Typegoose 的 isDocument and isDocumentArray。 应该是这样的:
let app: Application
const appName = 'appTest';
app = await (await this.findOne({ name: appName })).populate('component').execPopulate();
if (isDocument(app.component)) {
console.log(app.component.name);
}