'string' can't be used to index type '{}'
I have the following React component that generates an HTML Table from an array of objects. The columns that should be displayed are defined through the tableColumns
property.
When looping through items
and displaying the correct columns I have to use the key
property from the tableColumn
object ({item[column.key]}
) but typescript is generating 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 '{}'.
What could I do to fix this? I'm lost
How I call the component:
<TableGridView
items={[
{
id: 1,
name: 'John Doe',
email: '[email protected]'
},
{
id: 2,
name: 'Lorem ipsum',
email: '[email protected]',
}
]}
tableColumns={[
{
key: 'id',
label: 'ID',
},
{
key: 'name',
label: 'Name',
}
]}
/>
My Component:
export type TableColumn = {
key: string,
label: string,
};
export type TableGridViewProps = {
items: object[],
tableColumns: TableColumn[]
};
const TableGridView: React.FC<TableGridViewProps> = ({ tableColumns, items }) => {
return (
<table>
<tbody>
{items.map(item => {
return (
<tr>
{tableColumns.map((column, index) => {
return (
<td
key={column.key}
className="lorem ipsum"
>
{item[column.key]} // error thrown here
</td>
);
})}
</tr>
);
})}
</tbody>
</table>
);
}
Solution 1:
items: object[],
While technically it is a JavaScript object, the type can be better. For Typescript to correctly help you identify mistakes when accessing objects properties, you need to tell it the exact shape of the object. If you type it as object
, typescript cannot help you with that. Instead you could tell it the exact properties and datatypes the object has:
let assistance: { safe: string } = { safe: 1 /* typescript can now tell this is wrong */ };
assistance.unknown; // typescript can tell this wont really work too
Now in the case that the object can contain any sort of key / value pair, you can at least tell typescript what type the values (and the keys) have, by using an object index type:
items: {
[key: string]: number | string,
}[]
That would be the accurate type in the case given.
Solution 2:
If it's a pedantic javascript object that doesn't make sense to create type definitions per field for, and doesn't make sense as a class definition, you can type the object with any
and typescript will let you index however you want.
ex.
//obviously no one with two brain cells is going to type each field individually
let complicatedObject: any = {
attr1: 0,
attr2: 0,
...
attr999: 0
}
Object.keys(complicatedObject).forEach(key => {
complicatedObject[key] += 1;
}