Search code examples
typescriptany

Typescript - Element implicitly has an 'any' type because index expression is not of type 'number'.ts(7015)


I have this code that is returning an error that says : Element implicitly has an 'any' type because index expression is not of type 'number'.ts(7015)

const communes: ICommune[] = _.filter(wilayasData, (commune) => commune.wilaya_code === wilaya.wilaya_code);
      Object.keys(communes).forEach((key) => {
        communes[key].commune = communes[key].commune_name_ascii;
        communes[key].commune_ar = communes[key].commune_name;
        communes[key].daira = communes[key].daira_name_ascii;
        communes[key].daira_ar = communes[key].daira_name;
        delete communes[key].commune_name_ascii;
        delete communes[key].commune_name;
        delete communes[key].daira_name_ascii;
        delete communes[key].daira_name;
      });

the ICommune is an interface that is formed as follows :

    commune_name_ascii?: string;
    commune_name?: string;
    commune_ar?: string;
    commune?: string;
    daira?: string
    daira_ar?: string
    daira_name_ascii?: string;
    daira_name?: string;
    wilaya_code?: string;
    wilaya_name_ascii?: string;
    wilaya_name?: string;

Solution

  • I made couple changes in the interface to be :

    interface ICommune {
      commune_name_ascii?: string;
      commune_name?: string;
      commune_ar?: string;
      commune?: string;
      daira?: string
      daira_ar?: string
      daira_name_ascii?: string;
      daira_name?: string;
      wilaya_code?: string;
      wilaya_name_ascii?: string;
      wilaya_name?: string;
    }
    

    for the code I parsed the key as integer to be a number and meet the common index schema

          const communes: ICommune[] = _.filter(wilayasData, (commune) => commune.wilaya_code === wilaya.wilaya_code);
          Object.keys(communes).forEach((key) => {
            const k = parseInt(key, 10);
            console.log(k);
            console.log(key);
            communes[k].commune = communes[k].commune_name_ascii;
            communes[k].commune_ar = communes[k].commune_name;
            communes[k].daira = communes[k].daira_name_ascii;
            communes[k].daira_ar = communes[k].daira_name;
            delete communes[k].commune_name_ascii;
            delete communes[k].commune_name;
            delete communes[k].daira_name_ascii;
            delete communes[k].daira_name;
          });
    

    or we can just get the index besides the key

            communes[index].commune = communes[index].commune_name_ascii;
            communes[index].commune_ar = communes[index].commune_name;
            communes[index].daira = communes[index].daira_name_ascii;
            communes[index].daira_ar = communes[index].daira_name;
            delete communes[index].commune_name_ascii;
            delete communes[index].commune_name;
            delete communes[index].daira_name_ascii;
            delete communes[index].daira_name;
          });```