I've seen other StackOverflow answers, but none of them seem to be working. Here is the error I am getting:
Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'Paycheck'.
No index signature with a parameter of type 'string' was found on type 'Paycheck'.
I get this when I try to iterate over the Array<Paycheck>, and use their keys to get their value. Is there anything wrong with the way I have created my types?
const seperated: Record<string, Array<string>> = {
date: [],
taxes: [],
benefits: [],
retirement: [],
takeHome: [],
total: [],
};
type Paycheck = {
date: string;
taxes: number;
benefits: number;
retirement: number;
takeHome: number;
total: number;
};
// removing some code to go straight to forEach loop
// p is of type Paycheck
paychecks.forEach(p => {
// seperated is pretty much the keys of paycheck
Object.keys(seperated).forEach(key => {
// p[key] is throwing the error I mentioned before
seperated[key].push(p[key]);
});
});
What am I doing wrong? How do I add an index signature appropriately? I've looked at other solutions and can't get it to work.
seperated?