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

157
Views
Using the spread operator on an array of filters to create a Firestore query

I'm working on a filter component for my React web app and want to return (pass through props) an array of conditions for the Firestore query.

For example if the user wants to filter out posts with no comments, they check a checkbox and a where("commentsCount", ">", "0") is added to the array of filters.

  const queryParams = [
    collection(db, "posts"),
    orderBy(sortType, sortMethod),
    limit(POSTS_PER_PAGE),
  ];
  const filters = [
    where("commentsCount", ">", "0"),
    // And possibly more where's
  ];
  queryParams.push(...filters);
  
  // Create a firestore query object using the parameters
  const _query = query(...queryParams);

But doing this gives me a TypeScript error: A spread argument must either have a tuple type or be passed to a rest parameter. TS2556.

What am I doing wrong?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You cannot pass an argument of type (CollectionReference<DocumentData> | QueryConstraint)[] in query. Try adding collection() directly in query and removing it from queryParams so it'll be an array of type QueryContraint[]:

const queryParams = [
  orderBy(sortType, sortMethod),
  limit(POSTS_PER_PAGE),
];

const filters = [
  where("commentsCount", ">", "0"),
  // And possibly more where's
];
  
// Create a firestore query object using the parameters
const _query = query(collection(db, "posts"), ...queryParams);
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!