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

213
Visualizações
How to set typescript type for key value in objects?

I have an object of currency types with properties like this:

CurrencyType = {
  usd: {
    country: 'United States', 
    symbol: '$USD', 
    icon: <USD />
  }, 
  cad: {
    country: 'Canada', 
    symbol: '$CAD', 
    icon: <CAD />
  },
  etc...
}

How can I write the type so that it takes a dynamic value for the key of each object – usd, or cad

I am trying to find how I can set the typescript type for for this object above so that the object names are included.

Basically want something like this:

export interface CurrencyTypes {

   [currencyName: string]: {
      country: string;
      symbol: string;
      icon: React.ReactNode; 
   }
about 4 years ago · Santiago Gelvez
1 Respostas
Responde à pergunta

0

I believe you're looking for something like this where you iterate over the values of another type.


type CountryCurrency = "usd" | "cad" | "aud";

export interface CurrencyTypes {
   [Currency in CountryCurrency]: {
      country: string;
      symbol: string;
      icon: React.ReactNode; 
   }
}

This will require that any object that implements the CurrencyTypes interface has all of the keys that are possible values of CountryCurrency

about 4 years ago · Santiago Gelvez 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