-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Type 'CaseReducerWithPrepare' constrains its generic #688
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
Comments
With all the possible ways to type that there, there's unfortunately no possible way to infer that correctly. Instead, we are first allowing some pretty permissive types to enter almost everything and then use export type ValidateSliceCaseReducers<
S,
ACR extends SliceCaseReducers<S>
> = ACR &
{
[T in keyof ACR]: ACR[T] extends {
reducer(s: S, action?: infer A): any
}
? {
prepare(...a: never[]): Omit<A, 'type'>
}
: {}
} to make sure that the returned partial action of This requires for the For a usage example, see https://redux-toolkit.js.org/usage/usage-with-typescript#defining-action-contents-with-prepare-callbacks If you have any specific situations where that doesn't work, let us know. Also, all that is probably getting a lot easier once we get #637 on the road, as then you'll have an alternative notation like createSlice({
name,
initialState,
reducers: create => ({ // callback form
fetchTodos: create.asyncThunk(
todosAPI.fetchTodos,
{pending, fulfilled, rejected} // thunk reducers here
),
toggleTodo(state, action: PayloadAction<string>) { // "normal" reducer in this object
state[index].completed = !state[index].completed;
},
addTodo: create.preparedReducer( // this will be much better typed
(text) => ({payload: {text, id: nanoid()}}),
(state, action) => void state.push(action.payload);
)
}) which infers a lot more. |
Thank you for your kind explanation. Thank you for your kind explanation. It has been resolved. |
On
typings.d.ts
file of@redux/toolkit
, a genericAction
of typeCaseReducerWithPrepare
extends typePayloadAction
with default generics and constrains its methodprepare
to returnvoid
. However, a methodprepare
of a payload action seems able to returnany
. Does this type declaration fit the entity of the API?PayloadAction:
CaseReducerWithPrepare:
It looks like
<any>
is required afterAction extends PayloadAction
.The text was updated successfully, but these errors were encountered: