I'm relatively new to TypeScript. I have a need to implement two route guards that share a common function and state.
When I attempt to extend a injectable class with a constructor, the resulting class does not receive instances of the private members.
Contrived example:
// guard-base.ts
@Injectable()
export abstract class GuardBase {
constructor(
private sharedState: SharedState,
) {}
sharedMethod(): boolean {. . .}
}
// authentication-required.guard.ts
@Injectable()
export class RouteGuard extends GuardBase implements CanActivate {
canActivate(): Promise<boolean> {
console.log(this);
// this.sharedState equals undefined at this point
}
}
Do I have to provide the constructor arguments in the RouteGuard class? What I would like to do is encapsulate all of the imports and the shared method on the base class. Is that possible? If not, what's the best way to accomplish what I would like to do?