0

I got the following code that was a JS code and tried to convert it to a TS code:

const toCamelCase = (rows: any[]) => {
  return rows.map((row) => {
    const replaced = {};

    for (let key in row) {
      const camelCase = key.replace(/([-_][a-z])/gi, ($1) =>
        $1.toUpperCase().replace('_', '')
      );
      replaced[camelCase] = row[key];
    }

    return replaced;
  });
};

But I get the following error:

Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{}'.
  No index signature with a parameter of type 'string' was found on type '{}'.ts(7053)

Here:

  replaced[camelCase] = row[key];

How can I fix this?

best_of_man
  • 643
  • 2
  • 15
  • This should help you, [https://stackoverflow.com/questions/57086672/element-implicitly-has-an-any-type-because-expression-of-type-string-cant-b](https://stackoverflow.com/questions/57086672/element-implicitly-has-an-any-type-because-expression-of-type-string-cant-b) – Dorji Tshering Jan 02 '23 at 20:48

1 Answers1

1

You'll need add index signature to your replaced object like:

const replaced:{[index:string]:string} = {};
vanowm
  • 9,466
  • 2
  • 21
  • 37