Reputation: 99970
I have the following:
export class RootCtrl {
m: Model<any>;
constructor(m: Model<any>) {
this.m = m;
}
doFindMany(query: object, options?: CDTModelOpts, cb?: MongoErrorCB){
const Model = this.m;
// cb is optional, if cb == null, returns promise
const {populate, lean} = options || ({} as CDTModelOpts);
let q = Model.find(query);
if (populate && populate.length > 0) {
q = q.populate(populate);
}
if (lean !== false) {
q = q.lean();
}
return q.lean().exec(cb)
}
}
what I want to do, is declare a return type for this wrapper method...something like this:
doFindMany(query: object, options?: CDTModelOpts, cb?: MongoErrorCB) : Array<typeof this.m> {
const Model = this.m;
// cb is optional, if cb == null, returns promise
const {populate, lean} = options || ({} as CDTModelOpts);
let q = Model.find(query);
if (populate && populate.length > 0) {
q = q.populate(populate);
}
if (lean !== false) {
q = q.lean();
}
return q.lean().exec(cb)
}
the return type, is the type of the Model value that gets passed in. The syntax I use is totally bogus however. Is there a way for the return type to reflect the typeof an input parameter to the constructor?
Upvotes: 0
Views: 70
Reputation: 1467
Seems you need something like here.
export class RootCtrl<ModelType> {
m: Model<ModelType>;
constructor(m: Model<ModelType>) {
this.m = m;
}
doFindMany(query: object, options?: CDTModelOpts, cb?: MongoErrorCB): Array<Model<ModelType>>{
const Model = this.m;
// cb is optional, if cb == null, returns promise
const {populate, lean} = options || ({} as CDTModelOpts);
let q = Model.find(query);
if (populate && populate.length > 0) {
q = q.populate(populate);
}
if (lean !== false) {
q = q.lean();
}
return q.lean().exec(cb)
}
}
Upvotes: 1