Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

168
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!