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

113
Vistas
Declare recursivley method chaining in TypeScript

I have the following function that returns dynamic object in vanilla JS:

function myFunc(props) {
  return Object.keys(props).reduce((acc, key) => ({
    ...acc, [key](val) {
      console.log(val);
      return this;
    } 
  }), {});
}

And an object with that function above:

const myProps = {
  counter: 0,
  isCounting: false,
}

const obj = {
  myFunc: myFunc(myProps),
  anotherFunc: () => {},
  moreFunc: () => {},
}

And I want to decalre this object in my .d.ts file but I'm not sure what is the best way to define a type for recursive method chaining..

I tried using the following type:

type Obj<T> = {
  myFunc: () => Record<keyof T, () => this>;
  anotherFunc: () => void;
  moreFunc: () => void;
}

But the this keyword is referring to the type instance itself and not the myFunc object. Meaning I get also the anotherFunc and moreFunc in it instead of just the dynamic keys from myProps.

Can someone help with this implementation?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can define this in a TypeScript interface:

interface Obj {
    [key: string]: (val: string) => this
}

const obj: Obj = {
  foo(val) {
    console.log('foo:', val);
    return this;
  },
  bar(val) {
    console.log('bar:', val);
    return this;
  }
}

I've assumed that the val arguments are string.

Edit: I've just seen your comment on your post - you can capture the key dynamically with [key: string]. I've updated my answer.

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