I started learning typescript, so I'm creating situations to resolve, I'm getting error not sure why, see code above:
interface IObjct {
name: string,
city: string,
age: number,
}
const john = {
name: "John",
city: "London",
age: 24
};
const jerry = {
name: "Jerry",
city: "Rome",
age: 43
};
function filterPerson(arr: Array<IObjct>, term: string, key: string) {
return arr.filter(function(person) {
return person[key].match(term);
});
}
I'm getting error on line : return person[key].match(term);
person[key]
Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'IObjct'.
No index signature with a parameter of type 'string' was found on type 'IObjct'.ts(7053)
filterPerson([john, jerry], "London", "city");
String
orNumber
; usestring
andnumber
instead – jcalzString
but the deeper issue is thatkey
should probbaly be akeyof T
or a filtered version of that to allow theperson[key]
access – Titian Cernicova-Dragomirkey
should be of type"name" | "city"
and notkeyof IObjct
and notstring
and notString
. Not sure how far down the rabbit hole to go here. – jcalz