Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular - How to fix 'property does not exist on type' error?

I am following this video tutorial (text version of the same). I have followed exactly the same code and I am receiving this error:

error TS2339: Property 'getEmployees' does not exist on type 'EmployeeService'

I looked on the Internet and visited many questions on Stack Overflow like this, this, this and this, and so many others issues opened related to this error on GitHub.

Service:

//import statements go here ...  @Injectable() export class EmployeeService { private listEmployees: Employee[] = [     {       //just to avoid longer code, deleted dummy data.     },   ];    getEmployees(): Employee[] {       return this.listEmployees; //ERROR in src/app/employees/list-employees.component.ts(14,44)   } } 

Component class:

//import statements @Component({     selector: 'app-list-employees',     templateUrl: './list-employees.component.html',     styleUrls: ['./list-employees.component.css'] }) export class ListEmployeesComponent implements OnInit {     employees: Employee[];     constructor(private _EmployeeService: EmployeeService) { }      ngOnInit() {         this.employees = this._EmployeeService.getEmployees();     }  } 

I have imported service in app.module.ts and added it in providers array of ngModule.

I am not able to solve the error neither to understand what is causing this error.

like image 247
Gaurav Chauhan Avatar asked Apr 17 '18 17:04

Gaurav Chauhan


People also ask

How do you fix property does not exist on type?

The "Property does not exist on type '{}'" error occurs when we try to access or set a property that is not contained in the object's type. To solve the error, type the object properties explicitly or use a type with variable key names.

Does not exist on type string TypeScript?

The "Property does not exist on type String" error occurs when we try to access a property that does not exist on the string type. To solve the error, use an object instead of a string, or make sure you're accessing a valid built-in method on the string. Here is an example of how the error occurs.

Does not exist on type string TS 2339?

To fix the error "TS2339: Property 'x' does not exist on type 'Y'" with TypeScript, we should make sure the properties are listed in the interface that's set as the type of the object. interface Images { main: string; [key: string]: string; } const getMainImageUrl = (images: Images): string => { return images. main; };

Does not exist on type void?

The "Property does not exist on type void" error occurs when we try to access a property on the return value of a function that doesn't return anything. To solve the error, make sure to return the correct value from all of the function's code paths.


2 Answers

It usually happens when you develop Angular applications. To solve this just shut down the server & start it again:

$ ng serve  

Explanation

This happens because when you start the application, The server is actually serving the bundles(JavaScript/CSS/HTML... output files) stored in the dist folder. Sometimes, when you make changes in your code, the changes don't reflect in your bundles, which will lead to the server still using the old bundles.

like image 150
Melchia Avatar answered Sep 19 '22 07:09

Melchia


If you want to avoid the compilation warning then the dirty fix would be to make

employees: any[]; 

any instances allow any method to call any method on that object. This will avoid compilation warning but it's not runtime safe.

You need to be careful before using it. If you are sure that the method will be available at runtime then only use it.

like image 25
Chetan Laddha Avatar answered Sep 21 '22 07:09

Chetan Laddha