You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
exportable case reducers type, bugfix, documentation (#290)
* wip
* working state
* simplify types, remove ActionForReducer
* add documentation for types, remove obsolete `PayloadActions` type
* add release tags and type documentation
* specify all exports by hand
* add documentation and release tags
* add test "wrapping createSlice should be possible"
* don't use self-referencing generic restriction to support TS <3.4
* update comments
* CI please work
* Update src/createSlice.ts
Co-Authored-By: Mark Erikson <[email protected]>
* typos
* documentation
* prettier
* minor nitpick
* Update usage-with-typescript.md
* Update usage-with-typescript.md
Co-authored-by: Mark Erikson <[email protected]>
Copy file name to clipboardExpand all lines: docs/usage/usage-with-typescript.md
+79Lines changed: 79 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -195,6 +195,26 @@ createSlice({
195
195
})
196
196
```
197
197
198
+
### Defining the type of your `initialState`
199
+
200
+
You might have noticed that it is not a good idea to pass your `SliceState` type as a generic to `createSlice`. This is due to the fact that in almost all cases, follow-up generic parameters to `createSlice` need to be inferred, and TypeScript cannot mix explicit declaration and inference of generic types within the same "generic block".
201
+
202
+
Instead, you can use the construct `initialState: myInitialState as SliceState`.
### On the "type" property of slice action Reducers
199
219
200
220
As TS cannot combine two string literals (`slice.name` and the key of `actionMap`) into a new literal, all actionCreators created by createSlice are of type 'string'. This is usually not a problem, as these types are only rarely used as literals.
@@ -222,3 +242,62 @@ If you actually _need_ that type, unfortunately there is no other way than manua
222
242
### Type safety with `extraReducers`
223
243
224
244
Like in `createReducer`, the `extraReducers` map object is not easy to fully type. So, like with `createReducer`, you may also use the "builder callback" approach for defining the reducer object argument. See [the `createReducer` section above](#createreducer) for an example.
245
+
246
+
### Wrapping `createSlice`
247
+
248
+
If you need to reuse reducer logic, it is common to write ["higher-order reducers"](https://redux.js.org/recipes/structuring-reducers/reusing-reducer-logic#customizing-behavior-with-higher-order-reducers) that wrap a reducer function with additional common behavior. This can be done with `createSlice` as well, but due to the complexity of the types for `createSlice`, you have to use the `SliceCaseReducers` and `ValidateSliceCaseReducers` types in a very specific way.
249
+
250
+
Here is an example of such a "generic" wrapped `createSlice` call:
0 commit comments