Uncaught TypeError: Cannot read property 'value' of undefined
This is one of the worst problems web developers run into on a common basis. To add to it using frameworks will make it even more difficult. If you use hierarchy of components in your code, consider some hours of your time to be spent searching for that line of code that never returns the correct object or type. Most of the time, after many hours of debugging, I usually be like:
“What am I doing wrong ? What time is it ? Who am I?”
The point is, the larger the code is, the harder it is to find a very small line of code that caused all these confusions in the first place. Here is where I resorted to Typescript’s type checking. Consider the following example.
Typescript clearly shows me the error I made at compilation rather than compiling everything, building the code and making me debug line by line. This is just a small bit of code, so you can easily debug and resolve the issue. As the code becomes larger and more complex, it becomes harder for the developer to debug the code on a line by line aspect.
“One of the developers trying to fix the inconsistent code”
“Typescript compiler telling me it can’t compile stupid things”
The third one is not an issue, but more of an enhancement. Since Typescript follows a strict type based code, you can use Interfaces to make your code more clean and controlled. An interface is basically telling your code that whichever part of the code uses the Interface has to follow certain rules and has to have only certain properties. For example, imagine you are building an authentication module. You get the user data from your server. Now you can use an INTERFACE to tell Typescript that the user data will have certain properties and those properties will be of certain types. This will make sure that everywhere across the code, the user object will remain the same way without changing its properties and structure. Typescript also provides a read only mode, which on enabled in an Interface will never allow you to change the value of the property.
“Typescript, when you try to change the value of a readonly interface variable”
This will be very useful and your data and properties will remain consistent across the code.
“Well maybe not that long.”
Once you get the hang of it, things will be much easier!