I am trying to inject a service as a property into a class outside of the constructor (since this particular class requires an empty constructor) as below:
@injectable()
export class CustomAuthProvider implements interfaces.AuthProvider {
@inject(AUTH_TYPES.AUTH_SERVICE)
private readonly _authService: AuthService;
}
This gives the TS error: Property "_authService" has no initializer and is not definitely assigned in the constructor
I am curious if anyone has run into this issue and found a way around it. It would appear that the typescript compiler is throwing a warning because it doesn't know that the @inject decorator is going to provide the auth service, but as my testing confirms, the property injection is working as expected. Thanks in advance!
@annajolly have you tried with ! ?
@inject(AUTH_TYPES.AUTH_SERVICE)
private readonly _authService!: AuthService;
@annajolly another option in addition to @gejgalis would be to inject the service in the constructor.
@injectable()
export class CustomAuthProvider implements interfaces.AuthProvider {
constructor(@inject(AUTH_TYPES.AUTH_SERVICE) private readonly _authService: AuthService) {}
}
@annajolly I am going to close this. Please reopen if you have more problems
@dcavanagh @gejgalis Yes, I ended up using !, thank you!
I get Unable to resolve signature of parameter decorator when called as an expression.ts(1239)
When using it like:
constructor(@lazyInject(serviceIdentifiers.Window) private readonly windowService: Window) {
this.cachedPreferences = this.getInitPreferencesForCache();
}
Why?
While this is good:
constructor(@inject(serviceIdentifiers.Preference) private readonly preferenceService: Preference) {}
I ended up using !, too...
Most helpful comment
@annajolly have you tried with
!?@inject(AUTH_TYPES.AUTH_SERVICE) private readonly _authService!: AuthService;