I'm working in a .tsx file and am completely new to TS/React. I am trying to add a key/value pair to a StringMap at the very first index.
I have some code below that:
takes in the StringMap 'stats',
goes through each row and extracts clan from a string,
adds each clan to 'clans' (string[]),
creates a new stats map with clans 'statsWithClans'
function handlePlayerStatssWindow(stats: StringMap<string>)
{
const rawFields = Object.keys(stats).map((key, i) => (
<div key={i}>
{key}: {stats[key]}
</div>
));
var clans: string[] = [];
for (const [key, value] of Object.entries(stats))
{
const roles: string[] = value.split(',');
for(const role of roles)
{
if(role.includes('clan='))
{
clans.push(role.replace('clan=',''));
}
}
}
const statsWithClans = {...stats};
statsWithClans.clans = clans.toString();
// const statsWithClans = {clans: (clans.toString()), ...stats};
const formattedFields = Object.keys(statsWithClans).map((key, i) => (
<div key={i}>
{key}: {statsWithClans[key]}
</div>
));
}
The code works and outputs something like:
Name: Jeff
Class: Mage
Clans: Clan1, Clan2
If I try creating the map with clans at the start like so
const statsWithClans = {clans: (clans.toString()), ...stats};
Typescript complains saying:
Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{ groups: string; }'. No index signature with a parameter of type 'string' was found on type '{ groups: string; }'.ts(7053)
and 'statsWithClans[key]' is highlighted
StringMap defintion
export default interface StringMap<T> {
[index: string]: T;
}