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

212
Views
Cree una función en Typescript que, dado un objeto, devolverá un objeto que tiene claves y valores derivados de los valores del objeto dado

Estoy tratando de entender los tipos en Typescript. En general, va bien, pero estoy luchando para convertir esta función de Javascript a Typescript.

 export function keyed(object, key, value) { const newKey = object[key]; const newValue = object[value]; const newObject = {}; newObject[newKey] = newValue; return newObject; };

Este es mi intento hasta ahora:

 type ValueOf<T> = T[keyof T]; export function keyed<T, K extends keyof T>(object: T, key: K, value: K): { [key: ValueOf<T>]: ValueOf<T> } { const newKey: ValueOf<T> = object[key]; const newValue: ValueOf<T> = object[value]; const newObject: { [key: ValueOf<T>]: ValueOf<T> } = {}; newObject[newKey] = newValue; return newObject; };

Pero, estoy recibiendo algunos errores como:

 An index signature parameter type cannot be a literal type or generic type. Consider using a mapped object type instead.ts(1337)

y

 Type 'ValueOf<T>' cannot be used to index type '{}'.ts(2536)

He intentado usar definiciones DefinitelyTyped de guiones bajos para ayudar aquí , ya que hacen bastante de estas operaciones, pero no he tenido suerte. ¿Es imposible?

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!