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

116
Views
Declarar el encadenamiento de métodos recursivos en TypeScript

Tengo la siguiente función que devuelve un objeto dinámico en Vanilla JS:

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

Y un objeto con esa función arriba:

 const myProps = { counter: 0, isCounting: false, } const obj = { myFunc: myFunc(myProps), anotherFunc: () => {}, moreFunc: () => {}, }

Y quiero etiquetar este objeto en mi archivo .d.ts , pero no estoy seguro de cuál es la mejor manera de definir un tipo para el encadenamiento de métodos recursivos.

Intenté usar el siguiente tipo:

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

Pero this palabra clave se refiere a la instancia de tipo en sí y no al objeto myFunc . Lo que significa que también anotherFunc y moreFunc en lugar de solo las teclas dinámicas de myProps .

¿Alguien puede ayudar con esta implementación?

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

0

Puede definir this en una interfaz de TypeScript:

 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; } }

He asumido que los argumentos val son cadenas.

Editar: acabo de ver tu comentario en tu publicación: puedes capturar la clave dinámicamente con [key: string] . He actualizado mi respuesta.

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!