I am working with Typescript and firebase and I have a small abstraction layer with this function to search for a unique document base on its field name and its value.
where<K extends keyof (T & DocumentEntity)>(fieldName: K, operator: WhereFilterOp, value: unknown): Query<T> {
this.addCriterion(new WhereCriterion(fieldName as string, operator, value));
return this;
}
This works well when I want to query with a field at the base of the document, for example:
Model:
order: Order = {
orderId: baseId
item: { ... }
price: { ... }
restaurant: {
restaurantId: nestedId
name: chezGaston
}
}
Query:
const order = await this.documentPersistence.findUnique(
new Query<order>().where('orderId', '==', incomingOrderId)
);
But now I want to query base on the id of a nested object.
const order = await this.documentPersistence.findUnique(
new Query<order>()
.where('restaurant.restaurantId', '==', integration),
);
And this gives me a static error TS2345: Argument of type '"restaurant.restaurantId"' is not assignable to parameter of type 'keyof Order'.
How can I fix my function so it accepts Nested object as keyof my object?
I don't want to use // @ts-ignore