Search code examples
angularangular-reactive-forms

How to restrict property declaration while creating a FromGroup on angular reactive form?


I want to restrict property declaration while creating a FromGroup. I have tried this way.

  1. Create a type like this:

enter image description here

After that I have used this on FromGroup Generic type like this: enter image description here

But in this case I am getting this error: Type 'number' is not assignable to type 'AbstractControl<any, any>'

Is it really possible to restrict FormGroup ?


Solution

  • Form type should be like this.

    type FormType = { 
        id: FormControl<number | null>;
     }
    

    If you don't want to allow null values, then you can do this:

    type FormType = { 
        id: FormControl<number>;
     }
    
    assetDeAllocationForm = new FormGroup<FormType>({
       id: new FormControl(0, { nonNullable: true})
    });