Replies: 3 comments 7 replies
-
I'm not sure what you mean here. |
Beta Was this translation helpful? Give feedback.
-
@NetanelBasal
The consequence is always the same: creating a new type for each form. So before we were using directly our Domain types or modifying them by using type aliases like Pick, Omit, ... But being forced to create a new type for each form has a big impact when you have a lot of forms with nested types. Not saying it is wrong! I understand that this new approach is better! Just saying that the upgrade step is too big for us. Pragmatically, we would prefer to remove the types of forms and take the risk of having a bug vs duplicating all of our domain types and making our code hard to maintain. Just sharing kind of a patch approach with a custom /* eslint-disable */
import { AbstractControl } from '@angular/forms';
import { FormArray, FormControl, FormGroup } from '@ngneat/reactive-forms';
type NonUndefined<T> = T extends undefined | never ? never : T;
type IsNonUndefinedString<T> = NonUndefined<T> extends string ? T : never;
export type CustomControlsOf<
T extends Record<string, any>,
ForceFields extends { controlFields?: string; groupFields?: string; arrayFields?: string } = { controlFields: ''; groupFields: ''; arrayFields: '' }
> = {
[K in keyof T]: T[K] extends AbstractControl //already a control?
? T[K] //already a control!
: K extends IsNonUndefinedString<ForceFields['controlFields']> // force to be a control?
? FormControl<T[K]> // force to be a control!
: K extends IsNonUndefinedString<ForceFields['groupFields']> // force to be a group?
? FormGroup<T[K]> // force to be a group!
: K extends IsNonUndefinedString<ForceFields['arrayFields']> // force to be an array?
? FormArray<T[K]> // force to be an array!
: NonUndefined<T[K]> extends (infer R)[] //array type?
? FormControl<R[]> // array type! -> use control as default
: NonUndefined<T[K]> extends Record<any, any> // nested type?
? FormGroup<SgControlsOf<T[K], ForceFields>> // nested type! -> use group
: FormControl<T[K]>; //default case -> control
}; In this way, we can override the types of our fields without having to recreate a new type: form: FormGroup<CustomControlsOf<User, {controlFields: 'permissions'}>> In this way, permissions will be a Anyway, thx for the libs you are providing, we are using them a lot ;) |
Beta Was this translation helpful? Give feedback.
-
@NetanelBasal We still have troubles with the refactoring. So as explained above, we are using "our rules" to define what Control we want to generate: import { AbstractControl } from '@angular/forms';
import { FormArray, FormControl, FormGroup } from '@ngneat/reactive-forms';
type NonUndefined<T> = T extends undefined | never ? never : T;
type IsNonUndefinedString<T> = NonUndefined<T> extends string ? T : never;
export type SgControlsOf<
T extends Record<string, any>,
ForceFields extends { controlFields?: string; groupFields?: string; arrayFields?: string } = { controlFields: ''; groupFields: ''; arrayFields: '' }
> = {
[K in keyof T]: T[K] extends AbstractControl //already a control?
? T[K] //already a control!
: K extends IsNonUndefinedString<ForceFields['controlFields']> // force to be a control?
? FormControl<T[K]> // force to be a control!
: K extends IsNonUndefinedString<ForceFields['groupFields']> // force to be a group?
? FormGroup<T[K]> // force to be a group!
: K extends (IsNonUndefinedString<ForceFields['arrayFields']>) // force to be an array?
? (NonUndefined<T[K]> extends (infer R)[] ? FormArray<R, FormControl<R>> : never) // force to be an array!
: NonUndefined<T[K]> extends (infer R)[] //array type?
? FormControl<R[]> // array type! -> use control as default
: NonUndefined<T[K]> extends Record<any, any> // nested type?
? FormGroup<SgControlsOf<T[K], ForceFields>> // nested type! -> use group
: FormControl<T[K]>; //default case -> control
}; Then we use it like this to be compatible with the internal ControlsOf
By using this we can define that by default we want to use But when we want to use a We had the reflection to stop trying to fight against the default "rules" but we meet another issue: we use a lot the So on one side, we have a complex form structure but that matches perfectly the model structure What do you think? :) |
Beta Was this translation helpful? Give feedback.
-
I am writing this issue to share about the upgrade from v1.7.5 to v3.
We are using this library since the beginning in our mono repo and we really liked the fact that we could make our forms typed.
We are trying to keep our repo up to date but with the last upgrade but we investigated what will be the cost of refactoring our forms and it is really too much.
In fact, being forced to create a new type with specific AbtractControl each time we have a complex object is impacting us too much.
And because the upgrade cannot be incremental, we decided to wait until Angular will make its own version. Th the imports will not be the same and we could upgrade step by step.
And now we would like to upgrade to Angular v13 but you don't propose a v1.x compatible version :(
What could we do? Removing simply your library?
Beta Was this translation helpful? Give feedback.
All reactions