Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

788
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda