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

328
Vistas
Pass enum as generic to type?

I have enum:

enum A {
  a = 'a',
  b = 'b',
}

And a type:

type B<T = A> = {
   values: Record<T, string>; // error Type 'T' does not satisfy 
           //              the constraint 'string | number | symbol'.   

   // Type 'T' is not assignable to type 'symbol'.
}

Then I want to use it const someVar: B<someEnum>;.

Question: am I able to somehow pass enum as a generic to a type?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Yes, if you're ok with something like this:

type B<T extends A = A> = {
   values: Record<T, string>;
}

You won't be able to generalize enum though. Quoting the docs:

In addition to generic interfaces, we can also create generic classes. Note that it is not possible to create generic enums and namespaces.

about 4 years ago · Juan Pablo Isaza Denunciar

0

If you want to use the Enum keys of A, you need to extract them from the type it self.

Here is a sample on how you can achieve that:

enum A {
  a = 'a',
  b = 'b',
}

type B<T extends string = keyof typeof A> = {
   values: Record<T, string>;
  }

const object1 : B = {
values: {
  "a": "123",
  "b": "123"
}  
}
about 4 years ago · Juan Pablo Isaza 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