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

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