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

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

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