I am using for... in loop to iterate over an object using square brackets notation, TypeScript complains saying the following:
Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'EVENT'. No index signature with a parameter of type 'string' was found on type 'EVENT'.ts(7053)
I Know if this wasn't a loop I could tell TypeScript that "a" can be only certain values, but by using a loop I can't give types so I don't know what to do The problem is I think I can't tell Typescript that in event[a] "a" can take a few values only
interface EVENT {
imageURL: string;
artist: string;
location: string;
city: string;
seat: number;
direction: string;
country: string;
type: string;
date: string;
tickets_available: number;
tickets_left: number;
id: string;
description: string;
price: number;
}
data.filter((event: EVENT) => {
// a = key of the object data
for (let a in event) {
let aSplit =
typeof event[a] === "string"
? event[a].split(" ").map((element: string) => element.toLowerCase())
: event[a];
// b = word of searchTerm string
for (let b of querySplit) {
if (
a === "artist" ||
a === "location" ||
a === "city" ||
a === "country" ||
a === "type"
) {
if (aSplit.includes(b.toLowerCase())) matches.push(event);
}
}
}
});
I am using the latest typescript with the latest nextJS framework, tsconfig set to aim for ES2015 ScreenShot of the code
as (keyof EVENT)[]as an alternative