When you are working with Angular or only TypeScript, you may come across this error
Property ‘propertyName’ does not exist on type ‘never’
It can be little bit difficult to identify the problem especially if you are experiencing this for the first time as your program looks almost good.
In our scenario, it occurred while working with Angular.
We had a property called products in our service like this, whose value is empty array initially and we want to assign the data by fetching it from the server when the application initializes
We assigned the data to this property in AppComponent constructor
We injected the service in a different component and wanted to use products data in an *ngFor loop like this
Now, we got the above mentioned error Property ‘img’ does not exist on type ‘never’
As the thing we did was, one of the quite common requirement (fetching the data from the server and saving it in a service while initializing the application) and the error message was not providing more details regarding the exact cause of the error, we had to spend some time looking around for the solution.
Reason for the Problem:
The problem was causing because of TypeScript transpiler.
What was the Problem :
We have to provide Type to the property.
declare your property in your service like below ( it is advised to use an interface instead of any, which we used here for a quick fix)
Solution 2 (not recommended) :
If you are a busy programmer and knows exactly what you are doing and wants to save some time, you can change the value of “strict” to false in tsconfig.json file (not in angular.json file)
here are the package versions we had to get the above error message