Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parameterless reducer wrapped in undoableReducer always requires a parameter #13

Open
Trikas77 opened this issue Sep 9, 2024 · 2 comments

Comments

@Trikas77
Copy link

Trikas77 commented Sep 9, 2024

Using your example I tried to add a reducer with no parameters, as example setToSeven
When I now try to call the function via dispatch I always get:

TS2554: Expected 1 arguments, but got 0

It does work correctly if I pass any object, null or undefined. But it does not feel clean.

counterSlice.ts

import { createSlice } from "@reduxjs/toolkit";
import { createHistoryAdapter } from "history-adapter/redux";

interface CounterState {
  value: number;
}

const counterAdapter = createHistoryAdapter<CounterState>();

const { selectPresent, ...selectors } = counterAdapter.getSelectors();

const initialState = counterAdapter.getInitialState({ value: 0 });

export const counterSlice = createSlice({
  name: "counter",
  initialState,
  reducers: {
    setToSeven: counterAdapter.undoableReducer(
      (state) => {
        state.value = 7;
      },
    ),
  },
  selectors: {
    ...selectors,
    selectCount: (state) => selectPresent(state).value,
  },
});

export const {
  setToSeven
} = counterSlice.actions;

export const { selectCount, selectCanRedo, selectCanUndo, selectPaused } =
  counterSlice.selectors;

HistoryButtons.tsx

import { useAppDispatch } from "../../hooks";
import {
   setToSeven
} from "./counterSlice";

export function HistoryButtons() {
  const dispatch = useAppDispatch();

  return (
    <div className="card">
      <button onClick={() => dispatch(setToSeven())}>set to 7</button>
    </div>
  );
}
@EskiMojo14
Copy link
Owner

EskiMojo14 commented Sep 9, 2024

Thanks for the report! I'll investigate if I can improve the inference here - in the meantime a workaround would be to annotate the action type anyway:

export const counterSlice = createSlice({
  name: "counter",
  initialState,
  reducers: {
    setToSeven: counterAdapter.undoableReducer(
      (state, action: PayloadAction) => {
        state.value = 7;
      },
    ),
    // or
    setToSeven: counterAdapter.undoableReducer<PayloadAction>(
      (state) => {
        state.value = 7;
      },
    ),
  },
  selectors: {
    ...selectors,
    selectCount: (state) => selectPresent(state).value,
  },
});

It also works properly with the creator callback syntax:

export const counterSlice = createSlice({
  name: "counter",
  initialState,
  reducers: (create) => ({
    setToSeven: create.reducer(
      counterAdapter.undoableReducer((state) => {
        state.value = 7;
      }),
    ),
  }),
  selectors: {
    ...selectors,
    selectCount: (state) => selectPresent(state).value,
  },
});

Or creating it outside:

const setToSevenReducer = counterAdapter.undoableReducer(
  (state) => {
    state.value = 7;
  },
);

export const counterSlice = createSlice({
  name: "counter",
  initialState,
  reducers: {
    setToSeven: setToSevenReducer,
  },
  selectors: {
    ...selectors,
    selectCount: (state) => selectPresent(state).value,
  },
});

@EskiMojo14
Copy link
Owner

after some investigation, I'm not sure I'll be able to resolve this in a satisfactory manner - inference is a finnicky thing, and getting something that pleases this scenario appears to break others. It appears the reducers's constraint of Record<string, CaseReducer<State, PayloadAction<any>> is causing undoableReducer to infer the action type as PayloadAction<any>.

If I have any brainwaves I'll definitely come back to this, but hopefully in the meantime one of the workarounds above will be enough for your use case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants