-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Question: Typing thunkAPI without circular references #2237
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
Can you show the rest of the setup? Where and how are you using |
I think that's all the relevant redux-related code. I also have these hooks in another file:
And this is an example slice Code
As you can see, AsyncThunkConfig is used as the third type of createAsyncThunk. The errors I'm getting is "'state' is referenced directly or indirectly in its own type annotation" and "Type alias 'RootState' circularly references itself.", but it only happens if I use "state: RootState;" inside AsyncThunkConfig. I guess it's related to how I use typeof and ReturnType. |
One workaround might be to do Also, as a side note: you really should be using the "builder callback" form of |
Thanks for the tip. I tried using type It's weird, If I only declare the store objects and types (rootReducer, store, RootState, AppDispatch, AsyncThunkConfig), it works fine. But as soon as I do this:
Everything breaks and typescript starts complaining. Looks like createAsyncThunk does something with this generic type, which makes the store reference itself somehow. |
Frankly that's kinda weird :( This is why I asked for a sandbox or a repo, so we can poke at it ourselves. |
I created a code sandbox with the minimum code needed to reproduce this error: https://codesandbox.io/s/goofy-meitner-ls1hmf CodeSandbox won't display typescript errors, but after downloading the project and testing it locally, I get the same errors. |
@MarioUnlam if you use the notation that we recommend to use for TypeScript, the errors go away extraReducers: builder => {
// -------------- //
// Async Reducers //
// -------------- //
// Login
builder.addCase(login.fulfilled, (state, action) => {
state.isAuthenticated = true;
state.isLoading = false;
state.errorMessage = '';
})
builder.addCase(login.pending, (state, action) => {
state.isLoading = true;
state.errorMessage = '';
})
builder.addCase(login.rejected, (state, action) => {
state.isLoading = false;
state.isAuthenticated = false;
state.errorMessage = action.payload?.message;
})
}, |
Ah, nice. I was going to apply that notation anyways, but I didn't expect it to fix this issue. Thank you! |
I have this store config in two files:
And it works fine. However, I want to create a type for createAsyncThunk, so I can get a typed getState and extra. I tried this:
However, as soon as I create this type, typescript starts complaining that "state" and "dispatch" are being "referenced directly or indirectly in its own type annotation". I assume this happens because I'm trying to set the return type of getState, by infering the return type of getState. Isn't there a way to get the type of my state without ReturnType?
The text was updated successfully, but these errors were encountered: