Create a factory for actions using createAction not working #3361
-
I want to create a function that returns an action function using the
I want to do this to simplify the creation of actions in my project. I get the following error:
Any ideas of what I'm doing wrong? I'm using NgRx version 13. Thanks. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
I am not aware of a way to type this such that a generic declared in However, if you take in a "props config" as a parameter, then you could make a factory: const FEATURE_STATE_NAME = 'Some Feature';
function actionFactory<D extends string, P extends object>(description: D, /* include this param 👉 */ config: ActionCreatorProps<P> & NotAllowedCheck<P>) {
return createAction(`[${FEATURE_STATE_NAME}] ${description}`, config);
}
// 👇 then pass a props instance as an argument
const ExampleAction = actionFactory('Example Action', props<{ some: string }>());
// the action works as expected
const exampleInstance: { some: string; } & TypedAction<'[Some Feature] Example Action'> = ExampleAction({ some: 'value' }); |
Beta Was this translation helpful? Give feedback.
T
must be guaranteed to satisfy the constraintNotAllowedInPropsCheck
. This type guard excludes objects with atype
property, empty objects, and primitives.I am not aware of a way to type this such that a generic declared in
actionFactory
is guaranteed to pass this check. It only works with concrete object types.However, if you take in a "props config" as a parameter, then you could make a factory: