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

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

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