gameplate - v2.3.0
    Preparing search index...

    Function defineActions

    • Define a typed action map for a known state type S.

      Why is it called twice? defineActions<State>()(actions) is a curried call. The first call fixes S; the second infers each action's argument tuple. Without the split, TS would have to either widen state to unknown or force you to retype S in every action — either is worse.

      Type Parameters

      • S

      Returns <A extends ActionMap<S>>(actions: A) => A

      type State = { player: { x: number; y: number }; score: number };

      const actions = defineActions<State>()({
      move: (s, dx: number, dy: number) => ({
      ...s,
      player: { x: s.player.x + dx, y: s.player.y + dy },
      }),
      addScore: (s, points: number) => ({ ...s, score: s.score + points }),
      });

      // Later:
      const game = createGame({ state: initial, actions });
      game.actions.move(5, 0); // typed: (dx: number, dy: number) => void
      game.actions.addScore(100); // typed: (points: number) => void