3

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

Linda Paiste
  • 38,446
  • 6
  • 64
  • 102
cbdeveloper
  • 27,898
  • 37
  • 155
  • 336

1 Answers1

5

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.

phry
  • 35,762
  • 5
  • 67
  • 81
  • 1
    Yeah, `Draft` seems to work. It also works if I use my state type directly. Since those types are "identical" as far as containing the properties I'm allowed to update. But I'll use `Draft` to keep it more consistent. Thanks. – cbdeveloper Feb 16 '21 at 09:56
  • Yeah, those would only differ if your state contained readonly stuff. – phry Feb 16 '21 at 10:36