UNPKG

3.06 kBTypeScriptView Raw
1import { ActionReducer, Prettify, Primitive, Selector } from './models';
2import { MemoizedSelector } from './selector';
3export interface FeatureConfig<FeatureName extends string, FeatureState> {
4 name: FeatureName;
5 reducer: ActionReducer<FeatureState>;
6}
7type Feature<FeatureName extends string, FeatureState> = FeatureConfig<FeatureName, FeatureState> & BaseSelectors<FeatureName, FeatureState>;
8type FeatureWithExtraSelectors<FeatureName extends string, FeatureState, ExtraSelectors extends SelectorsDictionary> = string extends keyof ExtraSelectors ? Feature<FeatureName, FeatureState> : Omit<Feature<FeatureName, FeatureState>, keyof ExtraSelectors> & ExtraSelectors;
9type FeatureSelector<FeatureName extends string, FeatureState> = {
10 [K in FeatureName as `select${Capitalize<K>}State`]: MemoizedSelector<Record<string, any>, FeatureState, (featureState: FeatureState) => FeatureState>;
11};
12type NestedSelectors<FeatureState> = FeatureState extends Primitive | unknown[] | Date ? {} : {
13 [K in keyof FeatureState & string as `select${Capitalize<K>}`]: MemoizedSelector<Record<string, any>, FeatureState[K], (featureState: FeatureState) => FeatureState[K]>;
14};
15type BaseSelectors<FeatureName extends string, FeatureState> = FeatureSelector<FeatureName, FeatureState> & NestedSelectors<FeatureState>;
16type SelectorsDictionary = Record<string, Selector<Record<string, any>, unknown> | ((...args: any[]) => Selector<Record<string, any>, unknown>)>;
17type ExtraSelectorsFactory<FeatureName extends string, FeatureState, ExtraSelectors extends SelectorsDictionary> = (baseSelectors: BaseSelectors<FeatureName, FeatureState>) => ExtraSelectors;
18type NotAllowedFeatureStateCheck<FeatureState> = FeatureState extends Required<FeatureState> ? unknown : 'optional properties are not allowed in the feature state';
19/**
20 * Creates a feature object with extra selectors.
21 *
22 * @param featureConfig An object that contains a feature name, a feature
23 * reducer, and extra selectors factory.
24 * @returns An object that contains a feature name, a feature reducer,
25 * a feature selector, a selector for each feature state property, and
26 * extra selectors.
27 */
28export declare function createFeature<FeatureName extends string, FeatureState, ExtraSelectors extends SelectorsDictionary>(featureConfig: FeatureConfig<FeatureName, FeatureState> & {
29 extraSelectors: ExtraSelectorsFactory<FeatureName, FeatureState, ExtraSelectors>;
30} & NotAllowedFeatureStateCheck<FeatureState>): Prettify<FeatureWithExtraSelectors<FeatureName, FeatureState, ExtraSelectors>>;
31/**
32 * Creates a feature object.
33 *
34 * @param featureConfig An object that contains a feature name and a feature
35 * reducer.
36 * @returns An object that contains a feature name, a feature reducer,
37 * a feature selector, and a selector for each feature state property.
38 */
39export declare function createFeature<FeatureName extends string, FeatureState>(featureConfig: FeatureConfig<FeatureName, FeatureState> & NotAllowedFeatureStateCheck<FeatureState>): Prettify<Feature<FeatureName, FeatureState>>;
40export {};