-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
RFC add store.withCurriedTypes
helper
#684
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
Conversation
Deploy preview for redux-starter-kit-docs ready! Built with commit 0da887c https://deploy-preview-684--redux-starter-kit-docs.netlify.app |
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit 0da887c:
|
Well, it builds, but it has a bit of a bundling fuckup, so it's not testable right now :( |
Just wanted to drop this in here as a CSB preview - basic counter, thunk, asyncThunk, and extraArgument usage: https://codesandbox.io/s/latest-rtk-createthunk-curried-types-54cf4?file=/src/hooks.ts I wanted to add some thoughts/arguments to the // Just works, no additional ThunkAction types and less exposure to potentially
// confusing generics
export const multiplyByConditionally = createThunk(
(amount: number) => (dispatch, getState) => {
const {
counter: { value }
} = getState();
if (value < 1000) {
dispatch(actions.increment(value * amount));
}
}
);
/** Comparison without the createThunk helper
// needs to be a part of the default app setup
export type AppThunk = ThunkAction<void, RootState, unknown, AnyAction>
export const multiplyByConditionally = (
amount: number,
): AppThunk => async (dispatch, getState) => {
const { value } = getState();
if (value < 1000) {
dispatch(actions.increment(value * amount));
}
}
*/ |
Alternative naming idea for the "core" helper of this PR: type Store = typeof store;
const bindStoreTypes = initBindStoreTypes<Store>();
export const useAppDispatch = bindStoreTypes(useDispatch); |
Is this even still worth considering given our current TS usage guidance? |
Difficult to say 🤔 It adds an extra step to setup, but Also I do like the idea of a As for the R-R types: The hooks are probably fine with the guidance we have, but this also fixes the |
Gonna close that as this is not happening, but we should add some kind of currying helper at least to |
This might be a completely stupid idea. But it might also be very useful. I'm really not sure to be honest.
So what does this do?
It adds a
store.withCurriedTypes
method that takes either just one argument and returns it in a curried form, or a bunch of stuff in an object and returns it.So, you'd call it
This might easily be extendable for
createSelector
andcreateAsyncThunk
, but impossible forcreateSlice
(once we add the callback notation withcreateAsyncThunk
in).Downside: we're replicating a good chunk of the types of external libraries with minor modifications as part of RTK that way.
And at the moment, this might do stuff if react-redux is not present, but I can catch that - I was just lazy for this experiment as of now.
As I said, I'm not sure if this is a good idea, but I guess it's definitely worth discussing :)
An alternative route were to add the suggestion to do
which would have the same effect on
useDispatch
,useSelector
,connect
etc. - but would augment the original types, which might run the risk of type poisoning these interfaces if these augmenting definitions were shipped, e.g. in monorepos.