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

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

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

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 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