Redux-toolkit: `extraReducers` type problem

Created on 27 Mar 2020  路  5Comments  路  Source: reduxjs/redux-toolkit

Hi,

I wanted to check out the new createAsyncThunk and I stumbled with the following issue raised by TypeScript:
image

I can write [loadDates.fulfilled as any] to get rid of this error, but this feel very off.
Am I doing something wrong?

I am using "typescript": "3.8.3"

Most helpful comment

You could also use the type explicitly e.g [loadDates.fulfilled.type]

All 5 comments

https://redux-toolkit.js.org/usage/usage-with-typescript/#type-safety-with-extrareducers

@markerikson This seems to confuse people all the time - what are your thoughts on making the builder notation the default in the documentation?

You could also use the type explicitly e.g [loadDates.fulfilled.type]

He could, but then he wouldn't have inferred action types. Using the map object notation with TypeScript is pretty much useless.

I don't think we need to make it the _default_ in the docs, but I do think we should update both the createReducer and createSlice#extraReducers sections in the API reference with examples of both syntaxes, and emphasize there that the builder notation is preferred for TS usage.

Covered by #460 .

Was this page helpful?
0 / 5 - 0 ratings