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

225
Views
Typescript type: Object with static/fixed keys and dynamic keys

How do I create a typescript type for an object, where I can have a fixed set and dynamic set of keys? For example:

export type APIOMethods = 'getA' | 'getB' | 'getC';

export type item = {
  divId: string;
  role: string;
  [key: K in APIMethods]?: any;
};
// Not working โ†‘โ†‘โ†‘ ๐Ÿ”ด

export type item = {
  divId: string;
  role: string;
  [key: APIMethods]: any;
};
// Not working โ†‘โ†‘โ†‘ ๐Ÿ”ด

// the object could look like this:
{divId: '', role: '', getA: {}} OR 
{divId: '', role: '', getB: {}} OR 
{divId: '', role: '', getC: {}}
  • Typescript Playground Link here
about 4 years ago ยท Juan Pablo Isaza
1 answers
Answer question

0

Solution: using a mapped object type:

export type item = {
  [key in APIMethods]?: any;
} & {
  divId: string;
  role: string;
};

After fiddling around with the code more inside of Visual Studio Code, the editor ended up making a suggestion to use a mapped object type when I had type this earlier

export type item = {
  divId: string;
  role: string;
  [key: APIMethods]: any; 
  ^๐Ÿ”ด An index signature parameter type cannot be a literal type or generic type. Consider using a mapped object type instead.ts(1337)

};
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!