UNPKG

2.73 kBTypeScriptView Raw
1import { ActionCreator, ActionReducer, ActionType, Action } from './models';
2declare type ExtractActionTypes<Creators extends readonly ActionCreator[]> = {
3 [Key in keyof Creators]: Creators[Key] extends ActionCreator<infer T> ? T : never;
4};
5/**
6 * Return type of the `on` fn.
7 * Contains the action reducer coupled to one or more action types.
8 */
9export interface ReducerTypes<State, Creators extends readonly ActionCreator[]> {
10 reducer: OnReducer<State, Creators>;
11 types: ExtractActionTypes<Creators>;
12}
13/**
14 * Specialized Reducer that is aware of the Action type it needs to handle
15 */
16export interface OnReducer<State, Creators extends readonly ActionCreator[], InferredState = State, ResultState = unknown extends State ? InferredState : State> {
17 (state: unknown extends State ? InferredState : State, action: ActionType<Creators[number]>): ResultState;
18}
19/**
20 * @description
21 * Associates actions with a given state change function.
22 * A state change function must be provided as the last parameter.
23 *
24 * @param args `ActionCreator`'s followed by a state change function.
25 *
26 * @returns an association of action types with a state change function.
27 *
28 * @usageNotes
29 * ```ts
30 * on(AuthApiActions.loginSuccess, (state, { user }) => ({ ...state, user }))
31 * ```
32 */
33export declare function on<State, Creators extends readonly ActionCreator[], InferredState = State>(...args: [
34 ...creators: Creators,
35 reducer: OnReducer<State extends infer S ? S : never, Creators, InferredState>
36]): ReducerTypes<unknown extends State ? InferredState : State, Creators>;
37/**
38 * @description
39 * Creates a reducer function to handle state transitions.
40 *
41 * Reducer creators reduce the explicitness of reducer functions with switch statements.
42 *
43 * @param initialState Provides a state value if the current state is `undefined`, as it is initially.
44 * @param ons Associations between actions and state changes.
45 * @returns A reducer function.
46 *
47 * @usageNotes
48 *
49 * - Must be used with `ActionCreator`'s (returned by `createAction`). Cannot be used with class-based action creators.
50 * - The returned `ActionReducer` does not require being wrapped with another function.
51 *
52 * **Declaring a reducer creator**
53 *
54 * ```ts
55 * export const reducer = createReducer(
56 * initialState,
57 * on(
58 * featureActions.actionOne,
59 * featureActions.actionTwo,
60 * (state, { updatedValue }) => ({ ...state, prop: updatedValue })
61 * ),
62 * on(featureActions.actionThree, () => initialState);
63 * );
64 * ```
65 */
66export declare function createReducer<S, A extends Action = Action, R extends ActionReducer<S, A> = ActionReducer<S, A>>(initialState: S, ...ons: ReducerTypes<S, readonly ActionCreator[]>[]): R;
67export {};