Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2- Error: Type 'AbstractControl' is not assignable to type 'AbstractControl'

I am using the angular rc4 release to write an application where I have to impose few validation rules to form components like required, minlength along with some custom validator emailValidator.

When I pass one built in and one custom validator to the Validators.compose function, IDEs (both Webstorm & VS Code) display me compile time error messages like the one shown below:

enter image description here

However, you can see that in the above screenshot if both validators are built in, there is no error message.

The definition of my custom validator is given below:

static emailValidator(control: AbstractControl): {[key: string]: boolean} {
    if (control.value.match(/[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/)) {
      return null;
    } else {
      return {'invalidEmailAddress': true };
    }
}
like image 437
A J Qarshi Avatar asked Oct 29 '22 22:10

A J Qarshi


1 Answers

I solved the problem by replacing the type of control parameter from AbstractControl to Control as shown below:

static emailValidator(control: Control): {[key: string]: boolean} {
    if (control.value.match(/[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/)) {
      return null;
    } else {
      return {'invalidEmailAddress': true };
    }
}

Control class extends from AbstractControl. I still don't understand why I get this error message when I use FormControl or AbstractControl.

like image 63
A J Qarshi Avatar answered Nov 15 '22 05:11

A J Qarshi