Skip to content
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

Have to pass types explicitly in createAsyncThunk #2150

Closed
original001 opened this issue Mar 21, 2022 · 2 comments
Closed

Have to pass types explicitly in createAsyncThunk #2150

original001 opened this issue Mar 21, 2022 · 2 comments

Comments

@original001
Copy link

Hi! I want to use createAsyncThunk in my project, but there is some problems:

  1. Every time I want to use createAsyncThunk, I must pass types explicitly. Otherwise, I don't get types of state, extra, etc.
  2. If I pass my types, I lose type inferring of payloadCreator result. I need pass it explicitly too. Also, type of thunk argument moved to generic.

There are some redux libraries there is possible to redefine global interfaces. For example, react-redux do like that:

//  This interface can be augmented by users to add default types for the root state when
export interface DefaultRootState {}

This way is more convinient and can resolve these problems. I don't see any problems with that solution. In redux project is only one state.

In redux-toolkit I didn't find the same approach. ThunkApi depends on declare type AsyncThunkConfig, which I cannot redefine.

@original001 original001 changed the title createAsyncThunk demands on my types, that I must pass explicitly Have to pass types explicitly in createAsyncThunk Mar 21, 2022
@markerikson
Copy link
Collaborator

Correct. Per the instructions in the createAsyncThunk section of the "Usage with TypeScript" page, you must provide the types for the payload arg and the return value if you need to supply types for state and extra.

Frankly, a simpler option here is to just do const state = getState() as MyAppRootState. You're really not going to lose any meaningful type safety there.

Also, note that we're thinking about removing that DefaultRootState type from React-Redux itself, specifically because globally overriding types is an anti-pattern.

@phryneas
Copy link
Member

We are also thinking about adding an api for type pinning though, which will allow you to declare that only once per app: #1986 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants