[Typescript] Angular dependency injection and readonly

Hi guys,

We’re using TypeScript Rules in an Angular project and we have some problems withe the rule:

Private properties that are only assigned in the constructor or at declaration should be “readonly”

In fact, we use dependency injection in an angular component, as it’s done in angular.io documentation (https://angular.io/tutorial/toh-pt4#inject-the-heroservice) and in the angular styleguide (https://angular.io/guide/styleguide):

constructor(private heroService: HeroService) { }

For each injected dependency, we’re getting the major issue:

Private properties that are only assigned in the constructor or at declaration should be “readonly”

Actually, the rule encourages us to code:

constructor(private readonly heroService: HeroService) { }

It would maybe be relevant to remove this detection for angular projects as it make code heavy and it’s not the way angular’s team wants us to inject dependency.

Hi,

I understand why this rule is not applicable to your project. In this case we suggest you to not activate the rule. It’s not part of default Sonar Way profile, so that’s ok to not have this rule executed.