Update
- Opened issue for this question https://github.com/angular/angular/issues/34721
Intro
In Angular Services are provided using the decorator @Injectable
.
@Injectable() // -> works
export class MyService {
constructor() {}
}
Abstracting @Injectable
Before Ivy, it was possible to build an abstraction for @Injectable
(e.g. for configuring the provider dynamically, enhancing the service class).
The following snippet shows an example how @Injectable
can be wrapped.
function InjectableEnhanced() {
return <T extends new (...args: any[]) => InstanceType<T>>(target: T) => {
Injectable({ providedIn: "root" })(target);
};
}
Using the decorator InjectableEnhanced
(see above) does not work while Ivy is enabled.
The following code snipped causes a runtime error.
@InjectableEnhanced() // -> does not work
export class MyService {
constructor() {}
}
Runtime error
Compiling the service using @InjectableEnhanced
with angular/cli works, but the following error is shown in the browser. The corresponding project can be found at https://github.com/GregOnNet/ng-9-inject.git.
Maybe, the Angular compiler does some code transformation but is not able any more to resolve @Injectable inside other decorators.
Having a look at the angular repository, a reference to JIT-compiler can be found in injectable.ts
(see: https://github.com/angular/angular/blob/master/packages/core/src/di/injectable.ts#L14).
Question
Is there still a way abstracting @Injectable?