beekai-oss / little-state-machine

📠 React custom hook for persist state management
https://lrz5wloklm.csb.app/
MIT License
1.48k stars 53 forks source link

Type Error for actions without a payload. #148

Open numeroflip opened 1 year ago

numeroflip commented 1 year ago

I have issues with actions which have no payloads (eg. init or reset functions)

import { useStateMachine } from "little-state-machine";

export type GlobalState = {
  stateValue: string;
};

const defaultState: GlobalState = {
  stateValue: "value"
};

export default function Example() {
  const { actions } = useStateMachine({
    reset: () => {...defaultState} // <--- Action without payload
  });

  actions.reset()  // <--- Type Error: expects a payload argument 

  return null
}

image

Reason:

export declare type ActionsOutput<TCallback extends AnyCallback, TActions extends AnyActions<TCallback>> = {
    [K in keyof TActions]: (payload: Parameters<TActions[K]>[1]) => void;
};