javascript - כאשר אני מבצע אימות באפליקציית אנגולר שלי, אני נתקל בשגיאה No index signature with a parameter of type 'string' was found on type 'AbstractControl[]'
כאשר אני מבצע ולידציה באפליקציית האנגלר שלי אני נתקל בשגיאה הזו:
src/app/register/register.component.ts:45:39 - error TS7053: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'AbstractControl[] | { [key: string]: AbstractControl; }'.
No index signature with a parameter of type 'string' was found on type 'AbstractControl[] | { [key: string]: AbstractControl; }'.
45 return control?.value === control?.parent?.controls[matchTo].value ? null : {isMatching: true}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
זו השיטה שממנה נובעת השגיאה:
matchValues(matchTo: string): ValidatorFn {
return (control: AbstractControl) => {
return control?.value === control?.parent?.controls[matchTo].value ? null : {isMatching: true}
}
}
אני רק מתחיל עם Angular ו-TypeScript ואני לא יודע איך לתקן את זה. כל עזרה תתקבל בברכה.
תשובה #1
אתה צריך להגדיר איזה סוג אינדקס יש לאובייקט. במקרה שלך זהו אינדקס מבוסס מחרוזת.
matchValues(matchTo: string): ValidatorFn {
return (control: AbstractControl) => {
return control.value === (control?.parent?.controls as { [key: string]: AbstractControl })[matchTo].value ? null : { isMatching: true };
}
}
תשובה #2
פשוט התעלמתי מהשורה באמצעות/@ts-ignore וזה עובד בסדר.
