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

156
Visualizações
How to allow a function argument to dynamically only be one value from a typescript interface?

I am creating a theme handling function.

The function that has a desiredColor parameter. I want passed in argument to only be one of the keys from the interface provided as the type for the underlying colors.

Is there a way to restrict the desiredColor to only be one of the key values of the underlying interface?

(is there a way to do this without also having to maintain an adjacent enum?)

const handleThemeColors = (mode: ThemeMode): any => {
  const colorsHandler = (desiredColor: Array<keyof PaletteColors>) =>
  // goal ^ desiredColor can be either: blueDark, blueMain, or blueLight
 {
    switch (mode) {
      case 'light':
        return colorsLight[desiredColor];
      default:
        return colorsDark;
    }
  };
 ...
}
interface PaletteColors {
  blueDark: string;
  blueMain: string;
  blueLight: string;
}

const colorsLight: PaletteColors = {
  blueDark: 'rgba(2, 189, 185,1)',
  blueMain: 'rgba(0, 203, 198,1)',
  blueLight: 'rgba(147, 231, 229,1)'
}
about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Surely, it's as simple as

const colorsHandler = (desiredColor: keyof PaletteColors) =>
about 4 years ago · Santiago Trujillo 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