I notice that my project has a lot of resolver services that despite from working with different entities and repositories host the same code. So I took the challenge to reduce them to a single resolver service using generics:
@Injectable()
export class DetailResolver<T, R extends Repository<T>> implements Resolve<T> {
constructor(private repositoryService: R, private router: Router) {}
resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<T> {
// use repositoryService to resolve this route
}
}
The entity type T and the repository type R can be specified. However I have trouble using this service:
const appRoutes: Routes = [
// other routes
{
path: ':id',
component: MessageEditComponent,
resolve: {
message: DetailResolver<Message, MessageService>
}
}
];
@NgModule({
imports: [
RouterModule.forChild(appRoutes)
],
exports: [
RouterModule
],
providers: [
DetailResolver<Message, MessageService>
]
})
As soon as I specify the generic type in DetailResolver<Message, MessageService>
the compiler wants me to create an instance:
Value of type typeof DetailResolver is not callable. Did you mean to include new?
I am not really familiar with angular4 DI internals. Can anybody describe whats wrong, is there any solution to this problem? I am using angular 4.3.