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

77
Views
Pass rest of arguments in a given callback function in TypeScript in strict mode

I have a function A that takes another function as an argument. Inside function A I want to execute given function with one explicit parameter and rest with given parameter of a function. Like this.

function t(g: number, p: any, b: any): void {
    console.log(g)
    console.log(p)
    console.log(b)
}

function execute(fn: (t: number, ...args: any[]) => void) {

    // But this is not working...getting this error --- 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them 
    const ar = fn.arguments.slice(1)
    fn(3, ...ar)
}

execute(t)

How can I capture args execute with fn in strict mode?

about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

In addition to the fact that you're using strict mode, it doesn't make sense to use fn.arguments here since fn isn't being passed any arguments. Instead you can add an args parameter to execute and pass them to fn.

function t(g: number, p: any, b: any): void {
    console.log(g);
    console.log(p);
    console.log(b);
}

function execute<TRestArgs extends any[]>(fn: (g: number, ...args: TRestArgs) => void, ...args: TRestArgs) {
    fn(3, ...args);
}

execute(t, "p", "b");

I've added the TRestArgs type parameter to provide type safety. Now execute(t, "p", "b", "x") will raise an error because t only has three parameters.

about 4 years ago · Santiago Gelvez 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!