Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

155
Vistas
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 Respuestas
Responde la pregunta

0

Surely, it's as simple as

const colorsHandler = (desiredColor: keyof PaletteColors) =>
about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda