javascript - Când fac validarea în aplicația mea Angular, mă confrunt cu eroarea No index signature with a parameter of type 'string' was found on type 'AbstractControl[]'


Când fac validări în aplicația mea Angular, mă confrunt cu această eroare:

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}
                                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Aceasta este metoda de unde provine eroarea:

matchValues(matchTo: string): ValidatorFn {

   return (control: AbstractControl) => {

        return control?.value === control?.parent?.controls[matchTo].value ? null : {isMatching: true}
       }
    

}

Abia încep cu Angular și TypeScript și nu știu cum să rezolv asta. Orice ajutor ar fi apreciat.

Răspuns #1

Trebuie să definești ce tip de index are obiectul. În cazul tău, este un index bazat pe șiruri de caractere.

matchValues(matchTo: string): ValidatorFn {
    return (control: AbstractControl) => {
        return control.value === (control?.parent?.controls as { [key: string]: AbstractControl })[matchTo].value ? null : { isMatching: true };
    }
}

Răspunsul #2

Am ignorat pur și simplu linia folosind/@ts-ignore și funcționează bine.