import { Action } from "../types"; /** Unified action creator type */ export interface ActionCreator { (...args: TArgs): { type: TType; payload: TPayload; }; type: TType; match: (action: Action) => action is { type: TType; payload: TPayload; }; } /** Any action creator */ export type AnyActionCreator = ActionCreator; /** Map of action definitions */ export type ActionDefinitionMap = { [key: string]: true | string | ((...args: any[]) => any) | { type: string; prepare: (...args: any[]) => any; }; }; /** Helper to build prefixed type */ type PrefixedType = TPrefix extends "" ? TType : `${TPrefix}/${TType}`; /** Infer action creator type from a single definition with optional prefix */ type InferActionCreator = TDef extends true ? ActionCreator> : TDef extends string ? ActionCreator : TDef extends (...args: infer TArgs) => infer TPayload ? ActionCreator, TArgs, TPayload> : TDef extends { type: infer TType; prepare: (...args: infer TArgs) => infer TPayload; } ? TType extends string ? ActionCreator : never : never; /** Infer action creators map from definitions */ export type InferActionCreators = { [K in keyof TMap]: K extends string ? InferActionCreator : never; }; /** Infer action from action creator */ export type InferAction = T extends ActionCreator ? { type: TType; payload: TPayload; } : never; /** Infer action union from action creator map */ export type InferActionsFromMap = TMap extends Record ? InferAction : never; /** * Create action creators from a definition map. * * @example * ```ts * // Basic usage (no prefix) * const counterActions = actions({ * increment: true, // { type: "increment", payload: void } * decrement: "COUNTER_DECREMENT" as const, // { type: "COUNTER_DECREMENT", payload: void } * incrementBy: (n: number) => n, // { type: "incrementBy", payload: n } * set: { type: "SET", prepare: (v: number) => ({ value: v }) } * }); * * counterActions.increment(); // { type: "increment", payload: undefined } * counterActions.decrement(); // { type: "COUNTER_DECREMENT", payload: undefined } * counterActions.incrementBy(5); // { type: "incrementBy", payload: 5 } * counterActions.set(10); // { type: "SET", payload: { value: 10 } } * ``` * * @example * ```ts * // With prefix for namespacing (like RTK slices) * const todoActions = actions("todos", { * add: (title: string) => ({ title }), * remove: (id: number) => ({ id }), * toggle: (id: number) => id, * }); * * todoActions.add("Buy milk"); // { type: "todos/add", payload: { title: "Buy milk" } } * todoActions.remove(1); // { type: "todos/remove", payload: { id: 1 } } * todoActions.toggle(1); // { type: "todos/toggle", payload: 1 } * * // Custom string types are NOT prefixed (used as-is) * const appActions = actions("app", { * reset: "GLOBAL_RESET" as const, // { type: "GLOBAL_RESET" } - no prefix * }); * ``` * * @note When using custom string types, add `as const` for proper type inference: * ```ts * // ❌ Without `as const` - type is inferred as `string` * const bad = actions({ reset: "RESET" }); * // action.type is `string`, not `"RESET"` * * // ✅ With `as const` - type is inferred as literal `"RESET"` * const good = actions({ reset: "RESET" as const }); * // action.type is `"RESET"` * ``` */ export declare function actions(definitions: TMap): InferActionCreators; export declare function actions(prefix: TPrefix, definitions: TMap): InferActionCreators; export {}; //# sourceMappingURL=actions.d.ts.map