When I have a class like the following:
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>;
}
And assuming that the 'Component' class has a 'name' property, I can't do this:
let app: Application
const appName = 'appTest';
app = await (await this.findOne({ name: appName })).populate('component').execPopulate();
console.log(app.component.name);
because it gives me the following error:
Property 'name' does not exist on type 'Ref<Component, ObjectId>'.
Property 'name' does not exist on type 'ObjectId'
Is there a way I can have the linter perceive the type as T (from Ref<T>
) rather than ObjectId?
For now, what worked quite well for me is to use type guards, specifically the isDocument and isDocumentArray from Typegoose. It would be something like this:
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);
}