UNPKG

2.47 kBTypeScriptView Raw
1import { Reducer, Action, Types } from './type-helpers';
2declare type HandleActionChainApi<TState, TInputAction extends Action, TRootAction extends Action> = <TActionCreator extends (...args: any[]) => TInputAction, THandledAction extends ReturnType<TActionCreator>, TOutputAction extends Exclude<TInputAction, THandledAction>>(singleOrMultipleCreatorsAndTypes: TActionCreator | TActionCreator[], reducer: (state: TState, action: THandledAction) => TState) => [TOutputAction] extends [Action] ? Reducer<TState, TRootAction> & {
3 handlers: Record<Exclude<TRootAction, TOutputAction>['type'], (state: TState, action: TRootAction) => TState>;
4 handleAction: HandleActionChainApi<TState, TOutputAction, TRootAction>;
5} : Reducer<TState, TRootAction> & {
6 handlers: Record<TRootAction['type'], (state: TState, action: TRootAction) => TState>;
7};
8declare type HandleTypeChainApi<TState, TInputAction extends Action, TRootAction extends Action> = <TType extends TInputAction['type'], THandledAction extends Extract<TInputAction, Action<TType>>, TOutputAction extends Exclude<TInputAction, THandledAction>>(singleOrMultipleCreatorsAndTypes: TType | TType[], reducer: (state: TState, action: THandledAction) => TState) => [TOutputAction] extends [Action] ? Reducer<TState, TRootAction> & {
9 handlers: Record<Exclude<TRootAction, TOutputAction>['type'], (state: TState, action: TRootAction) => TState>;
10 handleType: HandleTypeChainApi<TState, TOutputAction, TRootAction>;
11} : Reducer<TState, TRootAction> & {
12 handlers: Record<TRootAction['type'], (state: TState, action: TRootAction) => TState>;
13};
14declare type GetAction<TAction extends Action, TType extends TAction['type']> = TAction extends Action<TType> ? TAction : never;
15declare type InitialHandler<TState, TRootAction extends Action> = {
16 [P in TRootAction['type']]?: (state: TState, action: GetAction<TRootAction, P>) => TState;
17};
18declare type RootAction = Types extends {
19 RootAction: infer T;
20} ? T : any;
21export declare function createReducer<TState, TRootAction extends Action = RootAction>(initialState: TState, initialHandlers?: InitialHandler<TState, TRootAction>): Reducer<TState, TRootAction> & Readonly<{
22 handlers: InitialHandler<TState, any>;
23 handleAction: [unknown] extends [TRootAction] ? any : HandleActionChainApi<TState, TRootAction, TRootAction>;
24 handleType: [unknown] extends [TRootAction] ? any : HandleTypeChainApi<TState, TRootAction, TRootAction>;
25}>;
26export {};