Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

170
Visualizações
Array filter in typescript?

I have to filter array in typescript.

const { movies, message } = useAppSelector(state => state.movies);

//Here movies is array getting from backend

Here I have to filter it in typescript.

I have code in javascript.

const mathches = movies.filter(movie => {
    const escapeRegExp = (str) => str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&")
    const regex = new RegExp(escapeRegExp(search), "gi");
    return movie.name.match(regex);
})

I can't understand how can define type here.

Here I get some error when I paste it in ts file-

Property 'filter' does not exist on type 'never'.
Parameter 'movie' implicitly has an 'any' type.
Parameter 'str' implicitly has an 'any' type.

Please help me to define type here.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You can define a type for movies:

interface MovieType { name: string }

Then change the filter as

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);
})

Edit: as per the question change

You can update movies as follows:

const movies = useAppSelector<MovieType[]>(state => state.movies);
about 4 years ago · Juan Pablo Isaza Relatório

0

Maybe try using this

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);
})
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda