Home > Mobile >  Element implicitly has an 'any' type because expression of type 'string' can
Element implicitly has an 'any' type because expression of type 'string' can

Time:01-03

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?

CodePudding user response:

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

const replaced:{[index:string]:string} = {};
  • Related