Search code examples
typescriptreduxreducersredux-toolkitimmer.js

How can I type a helper function parameter using WritableDraft from @reduxjs/toolkit?


I'm using @reduxjs/toolkit and I'd like to write a helper function for one of my slice reducers.

Something I would call like this:

reducers: {
  MY_REDUCER(draft, action) {
    helperFunction(draft);
  }
}

This would be the helper function:

const helperFunction= (
  draft: WritableDraft<MY_STATE_TYPE>
) : void => {
  // CHANGE draft
};

enter image description here

But I'm not being able to type the draft parameter. The WritableDraft type does not seem to be available from @reduxjs/toolkit. Is this possible? How can I do this?

@reduxjs/toolkit uses WritableDraft<MY_STATE_TYPE>

enter image description here


Solution

  • It seems like either immer recently renamed that or your IDE is just leaking an immer-internal name. Try the Draft type, which is what RTK is actually using.