I am having problems with TypeScript on this code which works well on normal JS
I get this error form TypeScript:
`Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{}'`
const data = [{
Department: 'HR',
Name: 'Tom'
},{
Department: 'Finance',
Name: 'Peter'
},{
Department: 'HR',
Name: 'Jane'
}]
const groups = {}
for (const { Name, Department } of data) {
if (!groups[Department]) groups[Department] = { title: Department, people: [] }
groups[Department].people.push(Name)
}
console.log(Object.values(groups))
.as-console-wrapper { max-height: 100% !important; top: 0; }
I have tried to add the Props
type Props = {
title?: string
people?: string[]
}
const groups: Props | undefined = {}
But I still get a similar error
`Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'Props'.`
groups: { [key: string]: { title: string, people: string[] } } = {};typescriptlang.org/play?#code/…const groups: { [key: string]: Props } = {}