2 Basic TypeScript Mistakes

 

This chapter covers

  • Using any too often, ignoring compiler warnings
  • Not using strict mode, incorrect usage of variables, and misusing optional chaining
  • Overusing nullish
  • Misusing of modules export and inappropriate use of type
  • Mixing up == and ===
  • Neglecting type inference

“You know that the beginning is the most important part of any work” said Plato. I add: “especially in the case of learning TypeScript”. When many people learn basics (any basics not just TypeScript) the wrong way, it’s much harder to unlearn them than to learn things from the beginning the proper way. For example, alpine skiing (which is also called downhill skiing, not to confuse with country skiing) is hard to learn properly. However, it’s easy to just ski with bad basics. In fact, skiing is much easier than snowboarding because you can two boards (skis) not one (snowboard). In skiing, things like angulation (the act of inclining your body and angling your knees and hips into the turn) don’t come easy. I’ve seen people who ski for years wrongly which leads to increase chance of trauma, fatigue and decrease control. We can extend the metaphor to TypeScript. Developers who omit the basics suffer more frustration (not a scientific fast, just my observation). By the way, why did the JavaScript file break up with the TypeScript file? Because it couldn't handle the “type” of commitment.

2.1 Using any Too Often

2.2 Ignoring Compiler Warnings

2.3 Not Using Strict Mode

2.4 Declaring Variables Incorrectly

2.5 Misusing Optional Chaining

2.6 Not Using Nullish Coalescing

2.7 Not Exporting/Importing Properly

2.8 Not Using or Misusing Type Assertions

2.9 Checking for Equality Improperly

2.10 Not Understanding Type Inference

2.11 Summary

sitemap