Search code examples
angularangular-reactive-forms

Angular reactive forms avoid null check


Can I define formControl's default value of type string instead string | null? So that compiler throws error, when formControl value set to null and not always throwing error if not checking for null? Why provide possible null for the input, so that parts in code might reset the value to null, if I don't want it?

<input type="text"
       matInput
       [formControl]="test">
    

test = new FormControl("");
...
const input = this.test.value ?? "";
this.doSomething(input);
...
doSomething(input: string){
  ...
}

Solution

  • there is a way to make a control non nullable. Then type should work better for your case

    new FormControl('', {nonNullable: true});