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

219
Views
Create function in Typescript that given an object will return an object that has keys and values derived from the values of the given object

I'm trying to wrap my head around types in Typescript. In general it's going well but I'm struggling to convert this function from Javascript to Typescript.

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

This is my attempt so far:

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

But, I'm getting a few errors like:

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

and

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

I've tried to use underscores DefinitelyTyped definitions to help out over here, since they do quite a bit of these operations but I haven't had any luck. Is it impossible?

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!