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

168
Views
Cannot access class before initialization

I'm creating a circular dependency between two classes which uses the same decorator for renaming some properties I get from a backend-service (backend-service properties are named in german and I want to rename them in a generic way):

// request.ts

export class Request {
    id: string;
    @JsonProperty('dienst')
    service: Service;
}
// service.ts

export class Service {
    id: string;
    @JsonProperty('letzteAntraege')
    lastRequests: Request[];
    @JsonProperty('beschreibung')
    description: string;
}

I'm using reflect-metadata to get the property-type within the decorator:

export function JsonProperty(serviceName: string) {
    return (target: Object, key: string) => {
        const keyType = Reflect.getMetadata("design:type", target, key);
   }
}

which leads to following error:

Uncaught ReferenceError: Cannot access 'Service' before initialization
    at Module.Service (request.ts:17:22)
    at Module.5115 (request.ts:17:14)
    at __webpack_require__ (bootstrap:19:1)
    at Module.6218 (request.ts:17:22)
    at __webpack_require__ (bootstrap:19:1)
    at Module.2226 (request.service.ts:10:23)
    at __webpack_require__ (bootstrap:19:1)
    at Module.9859 (services.component.html:10:71)
    at __webpack_require__ (bootstrap:19:1)
    at Module.3176 (main.js:15:97)

Any ideas how to avoid this?

Edit: So the problem (I guess) is that the decorators code is executed on initializing Request, which in this case is too early. It should only be executed after all types are initialized.

about 4 years ago · Juan Pablo Isaza
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!