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

128
Views
use object key as key for another object

I want to create a type in TypeScript and use values of another object for its keys

const routes = {
  home: "HOME",
  profile: "PROFILE"
}

export type NavigatorParamList = {
  [routes.home]: undefined;
  [routes.profile]: undefined;
};

I know that this code is not working! Any idea how to do it?

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

0

Your object's properties are variable strings and TypeScript won't let you use them as computed properties for a type unless you make them readonly literals with as const.

const routes = {
  home: "HOME",
  profile: "PROFILE"
} as const;
about 4 years ago · Juan Pablo Isaza Report

0

This is what String enums are for:

TS Playground

enum Route {
  Home = "HOME",
  Profile = "PROFILE"
}

// using a union of individual enum members:
type NavigatorParamList1 = Record<Route.Home | Route.Profile, undefined>; // { HOME: undefined; PROFILE: undefined; }

// or if you want to use all enum members:
type NavigatorParamList2 = Record<Route, undefined>; // { HOME: undefined; PROFILE: undefined; }

// or independently-defined members
type NavigatorParamList3 = {
  [Route.Home]: undefined;
  [Route.Profile]: undefined;
}
about 4 years ago · Juan Pablo Isaza Report

0

It's doable with mapped types and the new as clause:

const routes = {
  home: "HOME",
  profile: "PROFILE"
} as const;

type AcceptableKeyTypes = string | number | symbol;
type ObjectWithValuesWhichCanBeKeys = {[key: AcceptableKeyTypes]: AcceptableKeyTypes};
type ValuesAsKeys<T extends ObjectWithValuesWhichCanBeKeys> = {[P in keyof T as T[P]]: unknown};

let test: ValuesAsKeys<typeof routes> = {HOME: 7, PROFILE: 4};

TypeScript playground

I'm using here a generic type: ValuesAsKeys<T>.

The values of T need to be constrained to types which are acceptable as keys: string | number | symbol.

the [P in keyof T] part is a mapped type, which iterates over all keys of T. Then, the as clause mapes the key to a new value, in this case to T[P], which is the value of the current iterated key.

Lastly, the value is unknown, so you can use whatever new value you want. in the example I give, I'm using numbers as the new values, but it can be anything.

Note that as said in other answers, routes needs to have the as const assertion, so that the string values are considered as literal types, and can be then used as typed keys.

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!