I have this code in Angular-13:
export class ShowValidationErrorsComponent {
private static readonly errorMessages = {
'required': () => 'This field is required.',
'minlength': (params: { requiredLength: any; }) => `Value must be at least' ${params.requiredLength} characters long.`,
'maxlength': (params: { requiredLength: any; }) => `Value must be shorter than ${params.requiredLength} characters.`,
'pattern': (params: { requiredPattern: string; }) => 'The required pattern is: ' + params.requiredPattern
};
@Input() private control!: AbstractControlDirective | AbstractControl;
@Input() private shouldShow: boolean = true;
@Input() extraCssClasses: string = '';
showList(): boolean {
return !!(this.control &&
this.control.errors &&
this.shouldShow);
}
listOfErrors(): string[] {
let errors: string[] = [];
for (let field of Object.keys(this.control.errors!)) {
if (field in ShowValidationErrorsComponent.errorMessages) {
errors.push(ShowValidationErrorsComponent.errorMessages[field](this.control.errors![field]));
}
else if ('message' in this.control.errors![field]) {
errors.push(this.control.errors![field].message);
}
}
return errors;
}
}
which I used for validation. But I got this error:
Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{ required: () => string; minlength: (params: { requiredLength: any; }) => string; maxlength: (params: { requiredLength: any; }) => string; pattern: (params: { requiredPattern: string; }) => string; }'. No index signature with a parameter of type 'string' was found on type '{ required: () => string; min
This line of code is highlighted:
ShowValidationErrorsComponent.errorMessages[field]
in
errors.push(ShowValidationErrorsComponent.errorMessagesfield);
What do I do to get it resolve?
Thanks