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

147
Visualizações
Get property of initially empty object after adding properties

I am trying to make a data converting script (with Angular) and need to access properties of an object which is initially empty (see simplified code below). However, I get an error message that says that the key does not exist in the object. I can not add the keys when initialising the object because the data might be different in the next run. I have tried different methods of accessing the key but I have not yet figured it out. I hope someone can help me!

TS7053: Element implicitly has an 'any' type because expression of type '1' can't be used to index type '{}'.   Property '1' does not exist on type '{}'.

    data = {};
    const key = 1;
    for( let i = 0; i < 5; i++){
      Object.assign(data, {[i]: 'test'});
    }

    if(key in data){
      data[key] = 'test2';
      data.key = 'test2';
    }
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

If you don't know the property names at first, you can use any. Initialize it like this:

data: any = {};

If you know the property types (e.g. all the properties are strings), then you can type it something like this:

data: { [key: string]: string } = {};

Then it will work.

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