angularangular6encapsulationtslintangular2-hostbinding

no-unused-variable TSLint rule does not work with private @HostBinding


In my TSLint file, I have:

"no-unused-variable": true

In my components, I sometimes have:

// tslint:disable-next-line:no-unused-variable
@HostBinding('class') private classes = 'my-theme';

Because classes is private, TSLint complains, so I have to disable TSLint everytime.

I do not want to make @HostBinding public because of encapsulation.

What is the recommended way to solve this problem?


Solution

  • After some research, the solution is to simply make it public

    @HostBinding('class') public classes = 'my-theme';
    

    This is because from Angular's perspective, it accesses the component, something like component.classes. Therefore, semantically, it is public. It's the same reason why @Input should be public even if you don't use it in the template.

    Sources:

    https://stackoverflow.com/a/38469573/3481582