I am getting the following Typescript errors with the below validator method. I feel, I have tried everything, and I am out of ideas. Can anyone tell me how to solve the problem?
The validator method doesn't let the project compile and the problem is thus unrelated to the use of the method.
Type '(formArray: FormArray) => { required: boolean; } | null' is not assignable to type 'ValidatorFn'.
Types of parameters 'formArray' and 'control' are incompatible.
Type 'AbstractControl' is missing the following properties from type 'FormArray': controls, at, push, insert, and 6 more.
public static minSelectedCheckboxes(min: number = 1): ValidatorFn {
const validator: ValidatorFn = (formArray: FormArray) => {
const totalSelected = formArray.controls
// get a list of checkbox values (boolean)
.map((control) => control.value)
// total up the number of checked checkboxes
.reduce((prev, next) => (next ? prev + next : prev), 0);
// if the total is not greater than the minimum, return the error message
return totalSelected >= min ? null : { required: true };
};
return validator;
}
The method is taken from this example, which compiles, but it uses compilerOptions.strict = false, which is not acceptable. https://coryrylan.com/blog/creating-a-dynamic-checkbox-list-in-angular
Example in Stackblitz is here: https://stackblitz.com/edit/angular-98wn2v (fork of above example with compilerOptions.strict = true)