Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

237
Views
error TS7017: la firma de índice del tipo de objeto tiene implícitamente un tipo 'cualquiera' en la validación de formulario angular 2

Tengo un error de compilación durante la validación reactiva en angular 2 que está dando

error TS7017: la firma de índice del tipo de objeto tiene implícitamente un tipo 'cualquiera'

Para

 this.comErrors[field] = ''; const messages = this.validationMessages[field]; this.comErrors[field] += messages[key] + ' ';

se está ejecutando como debería ser, pero cuando intento ejecutar npm run build.prod, se produce un error y no se construye mi proyecto

aquí está mi código:

 onValueChanged(data ?: any): void { if (!this.companyAddForm) { return; } const form = this.companyAddForm; for (const field in this.comErrors) { // clear previous error message (if any) //errors occurs this.comErrors[field] = ''; const control = form.get(field); if (control && control.dirty && !control.valid) { const messages = this.validationMessages[field]; for (const key in control.errors) { this.comErrors[field] += messages[key] + ' '; } } } } comErrors = { code: "", name: "", address: "", subscribedOn: "", contactPerson: "", contactPhone: "" } validationMessages = { 'code': { 'required': 'Company Code is required.', 'minlength': 'Company Code must be at least 5 characters long.', 'maxlength': 'Company Code cannot be more than 10 characters long.' }, 'name': { 'required': 'Company Name is required.', 'minlength': 'Company Name must be at least 5 characters long.', 'maxlength': 'Company Name cannot be more than 25 characters long.' }, 'address': { 'required': 'Company Address is required.', 'minlength': 'Company Address must be at least 5 characters long.', 'maxlength': 'Company Address cannot be more than 25 characters long.' }, 'subscribedOn': { 'required': 'subscribe date is required.' }, 'contactPerson': { 'required': 'Mobile Number is required.' }, 'contactPhone': { 'required': 'Mobile Number is required.' } }
over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Esto se debe a que el compilador infiere el tipo de validationMessages y este tipo no es indexable .
El compilador necesita this.validationMessages a any , pero usted (probablemente) está usando el indicador --noImplicitAny que causa el error.

Puedes hacerlo:

 const messages = (this.validationMessages as any)[field];

O puede convertirlo en un tipo indexable:

 type ValidationMessage = { required: string; minlength?: string; maxlength?: string; } type ValidationMessages = { [name: string]: ValidationMessage; } class A { validationMessages: ValidationMessages = { ... } ... }
over 4 years ago · Santiago Trujillo Report

0

Para declarar un objeto en el que no tiene idea de cuáles serán los valores, pero sabe que las claves serán de tipo cadena:

 const myObject: {[key: string]: any} = {}
over 4 years ago · Santiago Trujillo Report

0

Usando la Destrucción de Objetos puedes arreglar con:

 const {[field]:messages} = this.validationMessages;
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!