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

261
Views
Spread Operator - TypeScript

I'm trying to pass an array as an argument with Spread operator but something is going wrong.

function addThreeNumbers(x:number, y:number, z:number){
  console.log(x+y+z)
}

const args: number[] = [2,6,4]

addThreeNumbers(...args)

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

0

It's exactly as the error says:

A spread argument must either have a tuple type or be passed to a rest parameter.

So either make args a tuple, such as with as const so it doesn't get widened to number[]:

const args = [2, 6, 4] as const;

Or make the parameters a rest parameter instead:

function addThreeNumbers(...args: number[]) {
    console.log(args[0] + args[1] + args[2])
    // or args.reduce((a, b) => a + b, 0)
}
about 4 years ago · Juan Pablo Isaza Report

0

For TypeScript to correctly predict what argument types are going to spread into the parameter, you will have to change the args variable type into a tuple as follows:

const args: [number, number, number] = [2, 6, 4];
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!