Present scenario: I am currently working on creating a type-safe wrapper for the node-mongodb driver. I am facing challenges in determining the return type for the project aggregation stage.
Feel free to check out the TypeScript Playground here
class BaseAggregate<Coll> {
private pipeline: Pipeline<Coll>[] = [];
constructor(initialPipeline?: typeof this.pipeline) {
if (initialPipeline) {
this.pipeline = initialPipeline;
}
}
match(stageInput: DeepPartial<Coll>) {
return new BaseAggregate<Coll>([...this.pipeline, { $match: stageInput }]);
}
projectActual(stageInput: Record<keyof Coll, 0 | 1>) {
return new BaseAggregate<T>>([
// what goes here ----^ instead of T?
// ...this.pipeline,
// { $project: stageInput },
]);
}
}
interface Test {
name: string;
email: string;
}
const agg = new BaseAggregate<Test>()
.match({ name: "john" })
.match({ email: "sir john" })
.match({ email: "sir" })
.projectActual({ email: 0, name: 1 })
Seeking assistance in defining the return type of the projectActual()
function in order to achieve the desired return type as shown in the example above.
interface {
name: string;
}
Your time and help are greatly appreciated 🙏