Search code examples
angulartypescriptformbuildertslinttsconfig

ts The property 'formBuilder' is declared but its value is never read


I used the following properties in my tsconfig.json file as complierOptions :

  "compilerOptions": {
    "noUnusedLocals": true,
    "noUnusedParameters": true
  },

I can see the unused variables underlined in my Visual Studio Code, and I can delete them, except for one variable that is used but it shows me the following message :

[ts] The property 'formBuilder' is declared but its value is never read.

I declared it like this :

constructor(private formBuilder: FormBuilder){
this.form = formBuilder.group({myFormControl: new FormControl()});
}

Solution

  • Remove the private modifier in the constructor parameters from front of the formBuilder or use via this - this.formBuilder.group.

    Modifier in the constructor parameter just creates a property on the component. Your code is equivalent to this

    private formBuilder: FormBuilder;
    
    constructor(formBuilder: FormBuilder) {
       this.formBuilder = formBuilder;
       this.form = formBuilder.group({myFormControl: new FormControl()});
    }