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

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

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