Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

129
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda