Tengo que filtrar la matriz en mecanografiado.
const { movies, message } = useAppSelector(state => state.movies); //Here movies is array getting from backendAquí tengo que filtrarlo en mecanografiado.
Tengo código en javascript.
const mathches = movies.filter(movie => { const escapeRegExp = (str) => str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&") const regex = new RegExp(escapeRegExp(search), "gi"); return movie.name.match(regex); })No puedo entender cómo se puede definir el tipo aquí.
Aquí aparece un error cuando lo pego en el archivo ts-
Property 'filter' does not exist on type 'never'. Parameter 'movie' implicitly has an 'any' type. Parameter 'str' implicitly has an 'any' type.Por favor, ayúdame a definir el tipo aquí.
Puede definir un tipo para las películas:
interface MovieType { name: string }Luego cambie el filtro como
const mathches = movies.filter((movie:MovieType) => { const escapeRegExp = (str: any) => str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&") const regex = new RegExp(escapeRegExp(search), "gi"); return movie.name.match(regex); })Editar: según el cambio de pregunta
Puede actualizar las películas de la siguiente manera:
const movies = useAppSelector<MovieType[]>(state => state.movies);Tal vez intente usar esto
const movies: Array<{name: string}> = [ {name: "The great wall"}, {name: "Moon Knight"}, {name: "Kong: Skull Island"}, {name: "Morbius"} ] const mathches = movies.filter((movie: any) => { const escapeRegExp = (str: any) => str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&") const regex = new RegExp(escapeRegExp(search), "gi"); return movie.name.match(regex); })