I have developed a unique approach to enhancing Firestore's Query class by implementing a Proxy wrapper. The role of my proxy is twofold:
- If a function is called on the proxy, which exists in the Query class, the proxy will direct that function call to the Query class.
- If the Query class returns another instance of itself (such as when using
.where()
), instead of returning the Query instance directly, my proxy envelops it in another layer of proxy. - On the other hand, if the return type is not another Query instance (for example, with
.onSnapshot()
), the result is seamlessly passed back to the method caller without additional wrapping.
- If the Query class returns another instance of itself (such as when using
- In addition to forwarding existing methods, my proxy also includes some custom methods for added convenience.
Initially, I defined the type structure as follows:
interface MyInterface { /* custom methods here */ }
type Store = MyInterface & FirebaseFirestore.Query
While this setup allows TypeScript to recognize query-method availability within the store, it does not explicitly convey that all query method return types are encompassed within the Store
type. How can I accurately convey this information to TypeScript?