gpt4 book ai didi

javascript - 当我在我的 Angular 应用程序中进行验证时,我面临在类型'AbstractControl []错误上找不到类型为 'string' 的参数的索引签名

转载 作者:行者123 更新时间:2023-12-05 01:09:16 26 4
gpt4 key购买 nike

当我在我的 Angular 应用程序中进行验证时,我遇到了这个错误:

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,但我不知道如何解决它。任何帮助将不胜感激。

最佳答案

你必须定义对象的索引类型。在您的情况下,它是基于字符串的索引。

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

关于javascript - 当我在我的 Angular 应用程序中进行验证时,我面临在类型'AbstractControl []错误上找不到类型为 'string' 的参数的索引签名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65353592/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com