I'm writing a typescript OData client query library and I'm stuck trying to get deep type inference to work. I have a query method that takes a query object that will be turned into a query string for the server request (mocked for this example). The expand property allows the caller to sub-query related entities and include them in the response.
Where im getting stuck is line 72 for the root of the issue, and line 51 for the error. Each expansion takes a function (for reasons described in comments) and the parameter is not able to be inferred (I think because typescript is having trouble with the function being an optional property).
Please let me know if you need more detail. I tried to document the code as best I could to describe my goals. I was worried a minimum reproduction would lose context and be more confusing to try and solve.
context:
OData is a http rest aligned standard for querying entities from an api. There is a tool that generates all the typescript models for me, but I wanted a library that helped me to:
- Build query strings in a type safe way
- Return type is the correct structure based on query
Being able to deeply infer the selections and expansions of the query is important because the api's return structure depends on what $select's (include entity properties) and $expand's (include related entities) you set.