What type should I use for a regular "payload-less" action when using Redux Toolkit with Typescript?

3.1k views Asked by At

Here is what I'm doing for my slice reducers when my actions require a payload:

import { createSlice, Action, PayloadAction } from "@reduxjs/toolkit";

type SOME_ACTION = PayloadAction<{ value: string }>

const SOME_SLICE = createSlice({
  name: "SOME_SLICE",
  initialState: getInitialState(),
  reducers: {
    SOME_ACTION(state, action: SOME_ACTION) {
      const { value }  = action.payload;
      state.someProp = value;
    }
  }
});

But some of my action currently don't require a payload. Although they don't require it right now, they might do in the future. So I would like to add some type to the action, even if I'm not currently using the action inside the reducer.

import { createSlice, Action, PayloadAction } from "@reduxjs/toolkit";

type SOME_ACTION = ???         // WHAT SHOULD I TYPE A "Payload-less" ACTION?

const SOME_SLICE = createSlice({
  name: "SOME_SLICE",
  initialState: getInitialState(),
  reducers: {
    SOME_ACTION(state, action: SOME_ACTION) {
      // CURRENTLY action IS NOT BEING USED HERE. BUT MIGHT BE IN THE FUTURE
      state.loading = true;
    }
  }
});

Is PayloadAction<void> the answer for this?

0

There are 0 answers