Typescript Ts7015: Element Implicitly Has an 'Any' Type Because Index Expression Is Not of Type 'Number'

Typescript Element implicitly has an 'any' type because expression of type 'number' can't be used to index

thx to reply bill.gates

(this.myobject as YourType)[i]._id;

This form works perfectly
Instead, I put any in the type.
It functions as I want

(this.myobject as any)[i]._id;

Is there a problem if I put any?

react typescript error - element implicitly has an any type because expression of type string cant be used to index type {}

reduce can take a generic to specify what the predicate returns (and the type of the initial value).

Here it is inferred as {} because of the initial value:

const result = trades.reduce((groupedAccounts, trade) => {
const account = trade.pacct;
if (groupedAccounts[account] == null) groupedAccounts[account] = [];
groupedAccounts[account].push(trade);
return groupedAccounts;
}, {});

You probably want it to be Record<string, TradeData[]>:

const result = trades.reduce<Record<string, TradeData[]>>((groupedAccounts, trade) => {
const account = trade.pacct;
if (groupedAccounts[account] == null) groupedAccounts[account] = [];
groupedAccounts[account].push(trade);
return groupedAccounts;
}, {});


Related Topics



Leave a reply



Submit