action-reducer
Version:
Type-safe ActionCreator and Reducer library
49 lines (48 loc) • 1.93 kB
TypeScript
export interface AnyAction {
type: any;
[extraProps: string]: any;
}
export interface Action<P, T extends string | symbol> {
type: T;
payload: P;
}
export interface ActionCreator<P extends unknown[], T extends string | symbol> {
(...args: P): Action<P, T>;
type: T;
}
export declare type Mutation<S, P extends unknown[]> = (state: Readonly<S>, ...payload: P) => S;
export interface CreateAction<S> {
<P extends unknown[], T extends string | symbol>(type: ActionCreator<P, T>, mutation: Mutation<S, P>): ActionCreator<P, T>;
<P extends unknown[]>(mutation: Mutation<S, P>): ActionCreator<P, string>;
<P extends unknown[], T extends string | symbol>(type: T | {
type: T;
}, mutation: Mutation<S, P>): ActionCreator<P, T>;
}
export interface CreateActionWithPrefix<S> {
<P extends unknown[], T extends string | symbol>(type: ActionCreator<P, T>, mutation: Mutation<S, P>): ActionCreator<P, T>;
<P extends unknown[]>(mutation: Mutation<S, P>): ActionCreator<P, string>;
<P extends unknown[], T extends string>(type: {
type: T;
}, mutation: Mutation<S, P>): ActionCreator<P, T>;
<P extends unknown[]>(type: string, mutation: Mutation<S, P>): ActionCreator<P, string>;
}
interface ActionReducer {
<S>(initState: undefined, prefix: string): {
createAction: CreateActionWithPrefix<S>;
reducer: (state: S, action: AnyAction) => S;
};
<S>(initState?: undefined): {
createAction: CreateAction<S>;
reducer: (state: S, action: AnyAction) => S;
};
<S>(initState: S, prefix: string): {
createAction: CreateActionWithPrefix<S>;
reducer: (state: S | undefined, action: AnyAction) => S;
};
<S>(initState: S): {
createAction: CreateAction<S>;
reducer: (state: S | undefined, action: AnyAction) => S;
};
}
declare const ActionReducer: ActionReducer;
export default ActionReducer;