Skip to content

TypeScript setup

The packages ship their own types; there’s nothing extra to install. This page covers what TypeScript checks for you, what it doesn’t, and how to close the gap.

Use a moduleResolution that reads a package’s exports map: bundler (Vite, Next, webpack), or node16 / nodenext. The legacy node setting (also called node10) can’t see the packages’ entry points. CI type-checks a consumer under both bundler and node16, with strict on.

tsconfig.json
{
"compilerOptions": {
"strict": true,
"moduleResolution": "bundler", // or "node16" / "nodenext"
"jsx": "react-jsx"
}
}

State types come from the values you pass in, or a type argument where there’s no value to infer from.

// Local: inferred from the initial state.
const { state, actions } = useLocalActions({ name: '', count: 0 });
state.count.toFixed(); // number
// Global: give the store your state type...
type AppState = { cart: { items: string[] }; _async: {} };
const store = createSimpleStore<AppState>({ cart: { items: [] }, _async: {} });
// ...and give selectors the state type and the selected type.
const items = useStore<AppState, string[]>((s) => s.cart.items);

A small typed wrapper saves repeating the state type on every useStore call:

export const useAppStore = <T,>(selector: (s: AppState) => T) =>
useStore<AppState, T>(selector);

Every action is an Action: a union of tuples whose first element names the action. Inline actions are checked where you write them:

<input onChange={actions(['action/save', ['name'], 'event/target.value'])} />

An action stored in a variable needs the type, or TypeScript widens it to an array of strings:

import { save, type Action } from '@input-actions/core';
const a = ['action/save', ['name'], 'Ada']; // (string | string[])[]: not an Action
const b: Action = ['action/save', ['name'], 'Ada']; // ok
const c = ['action/save', ['name'], 'Ada'] satisfies Action; // ok, keeps the literal type
const d = save(['name'], 'Ada'); // ok: helpers return Action
const list: Action[] = [b, ['action/toggle', ['subscribed']]];

as const doesn’t work here: it makes the tuple readonly, and Action tuples aren’t.

Action knows the shape of each tuple, not your state. ['action/save', ['nmae'], 'Ada'] type-checks. That’s deliberate: making Action generic over your state would list every valid path in the type system, and the cost grows with the size of your state type (ADR 0024 has the measurements). Paths are checked by a lint rule instead.

@input-actions/eslint-plugin checks every action path, and value, against your state type, using the type checker:

['action/save', ['draft', 'amont'], '1']
// ^^^^^^^ Path segment 'amont' does not exist on type …
['action/toggle', ['draft', 'note']]
// ^^^^^^^^^^^^^^^^^ action/toggle targets 'string' at draft.note — expected a boolean

It needs your state type exported by name. Setup, options and the full list of what it checks are on the lint plugin page.

useBoundActions(id) takes any string until you register your bindings record. Register it once, next to defineBindings:

lib/bindings.ts
export const appBindings = defineBindings({ /* ... */ });
declare module '@input-actions/core' {
interface Register {
bindings: typeof appBindings;
}
}

After that, ids, params and handler names are checked at every useBoundActions call. The bindings quickstart shows each check. getBoundActions from useLocalActions is typed from the record you pass it and needs no registration.

The declare module block has to be in a file TypeScript includes. If it isn’t imported anywhere, make sure your tsconfig.json include covers it.

Annotate machine definitions so the states and their actions are checked:

import type { StateMachineDefinition } from '@input-actions/core';
export const orderMachine: StateMachineDefinition = {
name: 'order',
initialState: 'editing',
states: {
editing: { allowedTransitions: ['submitting'] },
submitting: { allowedTransitions: ['done'] },
done: { allowedTransitions: [] },
},
};

An async handler receives the action’s extra arguments, then an AsyncHandlerContext with signal (an AbortSignal) and getState. Type the parameters yourself; the context takes your state type:

import type { AsyncHandlerContext, AsyncHandlers } from '@input-actions/core';
export const handlers: AsyncHandlers = {
loadUser: async (id: string, { signal }: AsyncHandlerContext) => {
const res = await fetch(`/api/users/${id}`, { signal });
return res.json();
},
saveCart: async ({ signal, getState }: AsyncHandlerContext<AppState>) => {
await fetch('/api/cart', { method: 'PUT', body: JSON.stringify(getState().cart), signal });
},
};

These are plain strings today. A typo shows up at runtime, as a warning or error in the console:

  • Machine names and mode names in machine/* actions.
  • Async handler names and operation ids in action/async* actions.
  • The arguments an action/async passes, against the handler’s parameters.

The lint rule skips them too. For machine-rooted bindings, validateBindings checks that every machine and mode they name exists, and the provider warns about them when it mounts; see Machine-rooted bindings.