I'm not sure if this is a possible bug in TypeScript or if it's working by design. I have an Angular project in which I have 3 classes. 1 abstract service, 1 service implementing the abstract service and 1 component using the implementing service.
Simplified, my classes look like this:
export abstract class BaseApiService {
constructor() { }
protected abstract baseUrl: string;
export class LogApiService extends BaseApiService {
baseUrl = environment.apiBaseUrl;
constructor() { }
}
export class LogPageComponent implements OnInit {
constructor(private logApiService: LogApiService) { }
ngOnInit(): void {
this.logApiService.baseUrl = 'why can I change this?';
}
}
Since i have added the protected keyword to the baseUrl property in BaseApiService, I'd expect the property to be mutable in LogApiService, but immutable in LogPageComponent. This is not the case. The code above in LogPageComponent's ngOnInit function compiles and runs without issue.
I can prevent LogPageComponent from being able to change LogApiService's baseUrl property by adding the protected keyword inside LogApiService like so: protected baseUrl = environment.apiBaseUrl;.
It feels like a bug to me that I have to add the protected keyword in both BaseApiService and LogPageComponent, but might of course be by design. Is there a reason for this behavior, or should I report it as a bug on Github in the TypeScript repo?