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

173
Visualizações
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 à 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