This is blog #2 in my series on how I'm teaching myself TypeScript. I cover things I've learned, provide links to resources I've found helpful, and share some personal best practices I've developed over the course of the past 3 months. The blogs make will make sense whether you read them in order or out of order, but if you're completely new to TypeScript, I suggest you start with Part I.
In this blog I cover things I've learned about:
- TypeScript's features
- Navigating the TypeScript ecosystem and configuring projects
- Developing personal best practices for working with TypeScript
- How I plan to continue furthering my TypeScript skills
h/t to this blog for the original image used as the base for this graphic
Two YouTubers that do a great job of showing real-world examples of when it makes sense to use various TypeScript features are Harry Wolff and Ben Awad. They both have multiple videos on specific TypeScript features where they walk through realistic code-snippets that show when it does and does not make sense to use certain features of the language.
That being said, watching a YouTube video or reading a code-snippet from documentation is not enough to fully grasp and internalize any given TypeScript feature and its utility. In order to really make the language work for you, it's important to experiment and build your own on projects, even if very simple. Projects will allow you to develop your own style, build confidence, and start to see firsthand the benefits of Typescript's extra features. That being said, it's not necessary to use all of a language's features in every project just because they exist. The only way to figure out which features make sense for the problem you're trying to solve is by building projects.
I'm certain there are some things I'm missing right now, but that's OK. The important thing for me is that I'm seeing benefits from using TypeScript, especially in my Node projects. I'm also learning more about computer science concepts and writing cleaner code as a result of working in TypeScript. So far I've only developed small solo projects, so I look forward to learning more about TypeScript's features as I begin to work on larger projects with other developers.
TypeScript is a compiled language, and can take some configuration before starting development on any given project. Depending on which packages you are trying to use in your project, you may need to set up Declaration Files or install pre-existing type declarations from npm. For example, if working with TypeScript and jQuery, you'll need to install
Sometimes, you may also need to change your
ts-config based on which packages and frameworks you're using. I suggest googling the name of the tech you want to use with "TypeScript + ts-config file" (e.g TypeScript and jQuery ts-config file) to find some settings that will work for your project rather than learning what each and every one of the options does when first getting started.
Though you can customize your ts-config file and install additional packages/compilers for your projects, there are also a lot of tools/frameworks that come with TypeScript support built in. If you're like me, you would rather spend your time writing code than configuring and customizing your project's setup, using solutions with great TypeScript support already built in is the way to go. I personally have found create-react-app and (for frontend projects) and Google Firebase (for building APIs) to be great solutions for simple TypeScript projects.
h/t Mark Pollman for this image
In the spirit of making the language work for me, I've become more comfortable with using the
any type at times as long as I use it sparingly and avoid using it for primitive types. Using the
any everywhere in your TypeScript code kinda defeats the whole purpose of the language, but using it once in a while in the interest of getting your program to "just work" is not a cardinal sin in my opinion.
There's no such thing as perfect code, and as long as you aren't writing software that requires extreme precision for something like a self-driving car or a medical device, attempting to write the most perfect code possible all the time can cause more harm than good and stifle progress. Your future-self will thank you for writing the cleanest code possible, but your future self will also never exist if you spend the rest of your life trying to debug an error that could be solved just by declaring an
I've been enjoying using TypeScript so far, and I'm excited to continue working with it and improving my skills. So far, I've learned about a lot of its features and using some of them even feels second nature at this point. If you're just getting your toes wet with TypeScript, or feel intimidated by it at all (like I was at first), then I encourage you to keep going because it gets easier with time and ultimately will improve aspects of your code and programming experience dramatically.
My next steps are:
- Learn typeORM and build an API that takes advantage of TypeScript's OOP features
- Build a project with Next.js + TypeScript
If you're also learning TypeScript I hope you found reading about my experience helpful. If you have any questions, suggestions for how to improve this blog, or if you feel I have gotten anything wrong please do not hesitate to leave a comment below or reach out to me via Twitter DM!