简体   繁体   English

在“{ [key: string]: AbstractControl; 类型上找不到带有“string”类型参数的索引签名 }

[英]No index signature with a parameter of type 'string' was found on type '{ [key: string]: AbstractControl; }

I'm currently working on an Angular project and defining a custom validator for a reactive form, and I'm having this error within the custom validators function, which being built.我目前正在开发一个 Angular 项目并为响应式表单定义一个自定义验证器,并且我在正在构建的自定义验证器 function 中遇到此错误。 Below are the code and the error received.下面是代码和收到的错误。

 initializeForm() {
    this.registerForm = new FormGroup({
      username: new FormControl('', Validators.required),
      password: new FormControl('', [Validators.required,
      Validators.minLength(4),
      Validators.maxLength(8)]),
      confirmPassword: new FormControl('', Validators.required)
    });
  }

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

The Error Message:错误信息:

Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{ [key: string]: AbstractControl; } | AbstractControl[]'.
  No index signature with a parameter of type 'string' was found on type '{ [key: string]: AbstractControl; } | AbstractControl[]'.

Can someone explain why this happens and how to handle it the correct way?有人可以解释为什么会发生这种情况以及如何以正确的方式处理它吗?

Regards,问候,

Try casting control?.parent?.controls to { [key: string]: AbstractControl;尝试将control?.parent?.controls为 { [key: string]: AbstractControl; } since it has two possible types. } 因为它有两种可能的类型。 If it's type is AbstractControl[] , then the index is not of type string, but of type number.如果它的类型是AbstractControl[] ,那么索引不是字符串类型,而是数字类型。

Something like this像这样的东西


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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我在我的 angular 应用程序中进行验证时,我面临在“AbstractControl [] 类型上找不到带有“字符串”类型参数的索引签名错误 - When I'm validating in my angular app I'm facing No index signature with a parameter of type 'string' was found on type 'AbstractControl[] Error Typescript: 在类型 '{ "A": string; 上找不到参数类型为 'string' 的索引签名 } - Typescript: No index signature with a parameter of type 'string' was found on type '{ "A": string; } 打字稿错误:在类型“{}”上找不到带有“字符串”类型参数的索引签名 - Typescript error : No index signature with a parameter of type 'string' was found on type '{}' 在“typeof Object”类型上找不到带有“string”类型参数的索引签名 - No index signature with a parameter of type 'string' was found on type 'typeof Object' 在 DataType 类型上找不到带有“字符串”类型参数的索引签名 - No index signature with a parameter of type 'string' was found on type DataType “字符串”类型的表达式不能用于索引“对象”类型。 在“对象”类型上找不到带有“字符串”类型参数的索引签名 - expression of type 'string' can't be used to index type 'Object'. No index signature with a parameter of type 'string' was found on type 'Object' TypeScript - 元素隐式具有“任何”类型 [...] 在类型上未找到具有“字符串”类型参数的索引签名 - TypeScript - Element implicitly has an 'any' type [...] No index signature with a parameter of type 'string' was found on type 打字稿 - 没有带有“字符串”类型参数的索引签名 - Typescript - No index signature with a parameter of type 'string' 类型“ string”的参数不能分配给“ AbstractControl”类型的参数。 角反应形式 - Argument of type 'string' is not assignable to parameter of type 'AbstractControl'. Angular Reactive Forms 在类型“{ order_graph_1: any; order_graph_2:任何; }' - No index signature with a parameter of type 'string' was found on type '{ order_graph_1: any; order_graph_2: any; }'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM