Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 508
cbdeveloper Avatar asked Oct 26 '25 13:10

cbdeveloper


1 Answers

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.

like image 161
phry Avatar answered Oct 29 '25 03:10

phry