UNPKG

2.85 kBTypeScriptView Raw
1import { ActionReducer } from './models';
2import { FeatureSelector, NestedSelectors } from './feature_creator_models';
3export declare type Feature<AppState extends Record<string, any>, FeatureName extends keyof AppState & string, FeatureState extends AppState[FeatureName]> = FeatureConfig<FeatureName, FeatureState> & FeatureSelector<AppState, FeatureName, FeatureState> & NestedSelectors<AppState, FeatureState>;
4export interface FeatureConfig<FeatureName extends string, FeatureState> {
5 name: FeatureName;
6 reducer: ActionReducer<FeatureState>;
7}
8declare type NotAllowedFeatureStateCheck<FeatureState> = FeatureState extends Required<FeatureState> ? unknown : 'optional properties are not allowed in the feature state';
9/**
10 * @description
11 * A function that accepts a feature name and a feature reducer, and creates
12 * a feature selector and a selector for each feature state property.
13 *
14 * @param featureConfig An object that contains a feature name and a feature reducer.
15 * @returns An object that contains a feature name, a feature reducer,
16 * a feature selector, a the selector for each feature state property.
17 *
18 * @usageNotes
19 *
20 * **With Application State**
21 *
22 * ```ts
23 * interface AppState {
24 * products: ProductsState;
25 * }
26 *
27 * interface ProductsState {
28 * products: Product[];
29 * selectedId: string | null;
30 * }
31 *
32 * const initialState: ProductsState = {
33 * products: [],
34 * selectedId: null,
35 * };
36 *
37 * // AppState is passed as a generic argument
38 * const productsFeature = createFeature<AppState>({
39 * name: 'products',
40 * reducer: createReducer(
41 * initialState,
42 * on(ProductsApiActions.loadSuccess(state, { products }) => ({
43 * ...state,
44 * products,
45 * }),
46 * ),
47 * });
48 *
49 * const {
50 * selectProductsState, // type: MemoizedSelector<AppState, ProductsState>
51 * selectProducts, // type: MemoizedSelector<AppState, Product[]>
52 * selectSelectedId, // type: MemoizedSelector<AppState, string | null>
53 * } = productsFeature;
54 * ```
55 *
56 * **Without Application State**
57 *
58 * ```ts
59 * const productsFeature = createFeature({
60 * name: 'products',
61 * reducer: createReducer(initialState),
62 * });
63 *
64 * const {
65 * selectProductsState, // type: MemoizedSelector<Record<string, any>, ProductsState>
66 * selectProducts, // type: MemoizedSelector<Record<string, any>, Product[]>
67 * selectSelectedId, // type: MemoizedSelector<Record<string, any, string | null>
68 * } = productsFeature;
69 * ```
70 */
71export declare function createFeature<AppState extends Record<string, any>, FeatureName extends keyof AppState & string = keyof AppState & string, FeatureState extends AppState[FeatureName] = AppState[FeatureName]>(featureConfig: FeatureConfig<FeatureName, FeatureState> & NotAllowedFeatureStateCheck<FeatureState>): Feature<AppState, FeatureName, FeatureState>;
72export {};