Closed astukanov closed 4 years ago
Eventually figured it out:
export const customTypesFormReducer = createReducer(
initialCustomTypesFormState,
onNgrxForms(),
on(addCustomType1, (state, type1) =>
updateGroup<CustomTypesForm>(state,
{
customTypes1: addArrayControl<CustomType1>(type1)
}
)
);
Providing the type is enough to make it work as desired. Although automatic type inference would be a bliss if possible.
Wow, yeah, that's a rather unfortunate error message that is hard to parse. In theory the lib should be able to infer this due to the type of type1
being inferred by ngrx and also you providing CustomTypesForm
to updateGroup
. The only thing you could try is to use the long form, i.e. the following. Let me know if this infers the type, and if not, what the IDE shows as the type for control
(it should be FormArrayState<CustomType1>
).
export const customTypesFormReducer = createReducer(
initialCustomTypesFormState,
onNgrxForms(),
on(addCustomType1, (state, type1) =>
updateGroup<CustomTypesForm>(state,
{
customTypes1: control => addArrayControl(control, type1)
}
)
);
Also, in the short form without the specified type for addArrayControl
, what does the IDE infer the generic type of addArrayControl
as?
Alright, I checked this myself. I see that the long form works as expected. The reason the short form does not work is an unfortunate design by ngrx that makes the second parameter an intersection between the props and the action type, which confuses the type inference. There is actually even a runtime impact since the object set in the form will have the type
property of the action itself. My suggestion is to use a dedicated property in the props:
export const addCustomType1 = createAction('[CUSTOM_FORM] ADD_CUSTOM_TYPE_1',
props<{ type1: CustomType1 }>()
);
export const customTypesFormReducer = createReducer(
initialCustomTypesFormState,
onNgrxForms(),
on(addCustomType1, (state, { type1 }) =>
updateGroup<CustomTypesForm>(state,
{
customTypes1: addArrayControl(type1),
}
)
),
);
With this code the correct value is set, type inference works as expected, and it is only a little bit more code.
@MrWolfZ Thanks a lot for taking a look at this and the provided approach. I will then switch to the solution with dedicated properties for my objects as it is definetly not that much of an overhead.
Hi, after after thorough research and many conclusions I reached an issue, which I am not able to solve. For the sake of consistency it would be best to use the same code style proposed by ngrx 10 actions in all of my reducers.
Here is a working example of my ngrx reducer:
Unfortunately I am already stuck with the first implementation of the add functionality to an array element with ngrx-forms.
Here is the code i wrote so far, which should work in my opinion after reading How to dynamically add formgroup controls to formarray in angular while the state is managed by ngrx-forms? and Dynamic Form Arrays Questions
I've tried a lot of variations to solve the issue, unfortunately non of them worked.
NgRx versions:
ngrx-forms version: