Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

326
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!