-
Notifications
You must be signed in to change notification settings - Fork 67
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor TypeScript to be generic on entities. #138
Refactor TypeScript to be generic on entities. #138
Conversation
Oh, and CC'ing @karevn who provided the original TS definitions recently, in case there's some reasoning behind the entities typing I've missed. Thanks! |
Thanks for your work! I'll take a closer look into later today. |
@karevn Thanks! In addition to the items noted in the original PR. I've pushed a few additional commits:
|
Well, it's definitely a valuable improvement and seems viable. I'll need some more time to think about it - to make sure these changes will be a flexible basis for future work and won't need an another pass of changes. |
@karevn Completely fair. I offered this PR as a starting point for discussion/consideration. I do think if we wanted to merge something like this, we might want to add defaults to the new Thanks again. |
Even though adding strict typing to Entities is a breaking change, I think it's worth it - I doubt that the TS adoption rate is high currently, so not too many people will be get hurt. And, they'll be able to add "any" typings at their side, which is a quite straightforward change. |
I will make a patch version on NPM so if people want the old typescript, they can use the previous version. |
Hey @petejohanson, The code looks great. If possible, before I merge this, I would like you or your team to pursue making this backward compatible as well. Other than that, I am happy with these changes |
@rctbusk Yeah, let me investigate doing that. I had just pushed a change before I saw your comment that updated the typing for
Let me switch the order of those, defaulting the I will update the current PR to probably rename the generic parameters to |
@rctbusk Ok, I've just pushed a change that adds defaulting to what I believe would be all of the places needed for backward compatibility with the generics changes. Unfortunately, this also now includes another breaking, but seemingly needed change, to the Also, doing it as an enum doesn't allow for custom HTTP methods, which is not great. I have instead changed this to be:
Doing so should get some nice auto-complete love in many IDEs, with suggestions for values, but also allow for other methods be allowed. So, to summarize:
Thoughts? |
Note: With the above changes dropped into our local app, combined with some work I'm doing to explore a openapi-generator generator to output typescript + redux-query, I now have a basic PoC for these type changes. Unfortunately, with the current very early stages of our work, it definitely doesn't exercise the full scope of the changes here. @karevn I just noticed you opened #139 recently... Thoughts on the const enum versus "open" union type I have here? |
@petejohanson and @karevn, I want to make sure both of your PRs are in sync with each other and make sense with each other. Want to make sure we follow best practices for Typescript and not just code for our specific needs in our products if that makes sense. I am new to typescript so I am learning here as we go and I am doing some of my own research on the side as well. Sorry if that makes the process a little slower. Want the best for the product :) |
* Change `UpdateStrategy<Entities>` to `UpdateStrategy<Entities, Body = ResponseBody>` to allow better strongly typed updates in consumers of the library.
a71bd64
to
cefa051
Compare
This looks good to me! |
Thanks! When do you anticipate a release that includes this? |
@petejohanson should be out now! |
As our team was exploring integrating redux-query into a new project using TS + React, we discovered the the built in type definitions in redux-query define the "entities state" very loosely, i.e.
type Entities = { [key: string]: any }
which really doesn't provide any time safety for the individual parts of the entities collection.This PR is my first pass at making most of the types generic in
T
, whereT
is the type of "entities" defined for your particular usage.With this PR, you might end up w/ something like:
And then your application state, would be:
With these changes, we can also strongly type the
update
, andtransform
props in the query config too, so those are strongly typed, includingupdate
only allowing callbacks that actually map to a property name in yourEntitiesState
and asserting the types parameter types for those callbacks matches, etc.One thing I didn't do in this PR was any attempt at defaulting the generic
Entities
type parameter back to the originalinterface Entities { [key: string]: any }
like it was before this PR.Theoretically, if you do some defaulting of that type parameter, this is at least more backwards compatible. I am happy to pursue that if the maintainer(s) are interested in this general approach.
Thoughts?