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

792
Views
How to solve error: a spread argument must either have a tuple type or be passed to a rest parameter

I'm developing wrapper class, which can call inner instance's method with string argument.

The whole code is like below.

class Inner {
  public prop1: string = 'publicProp';
  private prop2: string = 'privateProp';

  method1(param1: string) {
    console.log(`${param1}`);
  }
  method2(param1: number, param2: number) {
    console.log(`${param1}, ${param2}`);
  }
}

This class is original class have core functions.

type Fn = (...args: any) => any;

This is type needed in below

class Outer {
  private inner = new Inner();

  // I want proxy's second parameter's type will be changed according to first parameter's value
  public proxy<
    // F type is callable member's key
    F extends keyof {
      [K in keyof Inner as Inner[K] extends Fn ? K : never]: any;
    },
    // P type will be changed according to first argument type.
    P extends Parameters<Inner[F]>,
  >(name: F, ...args: P) {
    const fn = this.inner[name];
    if (typeof fn === 'function') {
      // This is right pain point!!
      // TS2556: A spread argument must either have a tuple type or be passed to a rest parameter.
      fn(...args);
    }
  }
}

The part of fn(...args) keeps showing error like above. I think, args should be inferred as tuple because we let the compiler know args's type will be extended by Parameter<Inner[F]>. How can I fix it?

const outer = new Outer().proxy;
// IDE can infer parameter's type from second parameter
outer('method1', 'stringArg');
outer('method2', 1, 2);

Another pain point is that the IDE can infer the parameter types according to first argument. (The real snapshot of IDE is attached below)

enter image description here

Thanks in advance your answer! :)

about 4 years ago · Juan Pablo Isaza
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!