(note here's a typescript playground with the example)
let filtered = items.filter(item =>
item.title.toLowerCase().includes(search.toLowerCase()) ||
item.name.toLowerCase().includes(search.toLowerCase()) ||
item.date.toLowerCase().includes(search.toLowerCase())
);
So I created this function
function filterByKey(items: T[], keys: K[], search: string) {
return items.filter((item) =>
keys.some((key) => item[key].toString().toLowerCase().includes(search.toLowerCase()))
);
}
to call it like this:
let filtered = filterByKey(items, ['title', 'name', 'date'], search));
But I'm getting this error:
Property 'toString' does not exist on type 'T[K]'
I tried like this, but it doesn't seem like a valid syntax
function filterByKey(items: T[], keys: K[], search: string)
how can I tell ts that the properties of items will support .toString()
or how would you handle such a case so taht typescript won't complain?
---
I checked this ts documentation:
https://www.typescriptlang.org/docs/handbook/2/generics.html#using-type-parameters-in-generic-constraints