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.