I want to filter my array of song by title and id but typescript throwing me error that property tolowercase() does not exit on type number
public querySongs() {
let song: Song[] = [];
song = this.song.filter((item) => {
return item.title.toLowerCase().indexOf(this.queryText.toLowerCase()) > -1 ||
item.id.toLowerCase().indexOf(this.queryText.toLowerCase()) > -1
});
this.song = song;
}
filtering by title works fine but by id keeps given me this error
"Property 'toLowerCase' does not exist on type 'number'"
I think the error is clear enough, the item is dynamically cast as number type. Try to cast it to string by appending .toString()
Instead of
return item.title.toLowerCase()...
Change it like this
return item.title.toString().toLowerCase()
or you can use ternary to identify whether it's string type or not
return typeof item.title === 'string' ? item.title.toLowerCase() : item.title