UNPKG

3.05 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}
13export interface OnReducer<State, Creators extends readonly ActionCreator[]> {
14 (state: State, action: ActionType<Creators[number]>): State;
15}
16/**
17 * @description
18 * Associates actions with a given state change function.
19 * A state change function must be provided as the last parameter.
20 *
21 * @param args `ActionCreator`'s followed by a state change function.
22 *
23 * @returns an association of action types with a state change function.
24 *
25 * @usageNotes
26 * ```ts
27 * on(AuthApiActions.loginSuccess, (state, { user }) => ({ ...state, user }))
28 * ```
29 */
30export declare function on<State, Creators extends readonly ActionCreator[]>(...args: [
31 ...creators: Creators,
32 reducer: OnReducer<State extends infer S ? S : never, Creators>
33]): ReducerTypes<State, Creators>;
34/**
35 * @description
36 * Creates a reducer function to handle state transitions.
37 *
38 * Reducer creators reduce the explicitness of reducer functions with switch statements.
39 *
40 * @param initialState Provides a state value if the current state is `undefined`, as it is initially.
41 * @param ons Associations between actions and state changes.
42 * @returns A reducer function.
43 *
44 * @usageNotes
45 *
46 * - Must be used with `ActionCreator`'s (returned by `createAction`). Cannot be used with class-based action creators.
47 * - The returned `ActionReducer` should additionally be wrapped with another function, if you are using View Engine AOT.
48 * In case you are using Ivy (or only JIT View Engine) the extra wrapper function is not required.
49 *
50 * **Declaring a reducer creator**
51 *
52 * ```ts
53 * export const reducer = createReducer(
54 * initialState,
55 * on(
56 * featureActions.actionOne,
57 * featureActions.actionTwo,
58 * (state, { updatedValue }) => ({ ...state, prop: updatedValue })
59 * ),
60 * on(featureActions.actionThree, () => initialState);
61 * );
62 * ```
63 *
64 * **Declaring a reducer creator using a wrapper function (Only needed if using View Engine AOT)**
65 *
66 * ```ts
67 * const featureReducer = createReducer(
68 * initialState,
69 * on(
70 * featureActions.actionOne,
71 * featureActions.actionTwo,
72 * (state, { updatedValue }) => ({ ...state, prop: updatedValue })
73 * ),
74 * on(featureActions.actionThree, () => initialState);
75 * );
76 *
77 * export function reducer(state: State | undefined, action: Action) {
78 * return featureReducer(state, action);
79 * }
80 * ```
81 */
82export declare function createReducer<S, A extends Action = Action>(initialState: S, ...ons: ReducerTypes<S, ActionCreator[]>[]): ActionReducer<S, A>;
83export {};