Tracking Type Coverage in Typescript - Why? - All Things Typescript Newsletter - Issue #18
Good afternoon; I hope your week started very well. Last week’s issue didn’t go out as I couldn’t complete it in time; I spent part of that weekend at the first ever Angular Kenya physical meet and greet, where I am a mentor and got to interact with these fantastic Software Engineers.
In this issue, I want to talk about Type Coverage and why you should keep track of it in your code. On top of that, as usual, I have curated articles and tweets from other content creators that I hope you will find helpful when learning Typescript.
As always, I hope you have a great week and as always, keep on learning.
Tracking Type Coverage in Typescript - Why?
Type Safety is essential in Typescript, and one thing that compromises Type Safety is the any
type. This is because the any
type is the broadest type covering all values and can be assigned to any other variable. When you use any, you are essentially turning off type checking. Please check out this article I wrote a while back on any vs. unknown.
There are several sources where the any
type can be introduced into your codebase:
When we explicitly type a variable as
any
,Implicitly any time, i.e., when Typescript infers your type as any because it couldn’t determine any other type, and
From third-party libraries you are using will introduce any into your code
From your JS Code (if you have a mixed codebase of both Javascript and Typescript)
All the above scenarios are likely to happen for large codebases, and it’s wise to keep track of your type coverage to prevent regressions in Type Safety. As the codebase grows, you are sure to add more type-safe code in your codebase and not compromise type safety.
So, what do you use? There is a package called - type-coverage - which you can use to track code coverage. It’s easy to get started; all you have to do is go to a project and just run the following command:
npx type-coverage
Here are the results of one of my runs on the NPM Imported Packages Lens, my VS Code extension for Typescript and Javascript.
There are a number of configurations you can enable, either via command flags or package.json
such as failing when the target type coverage isn’t reached. This can also be integrated with CI checks to ensure that type coverage regression is caught early on and builds halted.
For more information, please visit the docs here.
Join All Things Typescript Discord Server
Are you looking to continue learning Typescript? I have created an All Things Typescript Discord Server where we can all learn together. On Wednesday evening, we will have a fireside chat event where we can discuss this issue and any burning questions/discussions you may want to ask. We also have a channel for discussions, questions, mentorship, etc., that you can utilize any time, and the community will jump to help you in any way they can.
Articles
6 tips to make your JS/TS repository more new-joiners friendly
Have you ever wondered how to make your repository more readable and lower the entry-level for new joiners in your team? How to decrease the time needed for onboarding new people and limit questions asked by external people who want to use your solutions or contribute to them? Or how to make it easi…
Using TypeScript Mapped Types Like a Pro
Have you used the Partial, Required, Readonly, and Pick utility types? Do you know how they work internally? If you want to master them thoroughly and create your own utility types, don’t miss the content covered in this article.
A declarative approach to error handling in Typescript.
Sooner or later, your application will encounter some errors. In a perfect world, we would like to catch this error and respond accordingly. Sadly, as our codebase grows, it gets more difficult to manage errors in our applications efficiently.
Run Third-Party Scripts From A Web Worker
Partytown is a lazy-loaded library to help relocate resource intensive scripts into a web worker, and off of the main thread. Its goal is to help speed up sites by dedicating the main thread to your code, and offloading third-party scripts to a web worker.
Your Code Doesn't Have to Be a Mess
Fools ignore complexity. Pragmatists suffer it. Some can avoid it. Geniuses, remove it. - Alan Perlis If you’ve been developing software for a while, you know that code has this natural tendency to turn into a mess. Keeping software simple over time is a challenge that keeps me thinking.
JavaScript replaceAll() – Replace All Instances of a String in JS
Learn to code for free. freeCodeCamp’s open source curriculum has helped more than 40,000 people get jobs as developers.
JavaScript document.ready() – Document Ready JS and jQuery Example
Learn to code for free. freeCodeCamp’s open source curriculum has helped more than 40,000 people get jobs as developers. Get started
12 Rarely Used JavaScript Web APIs that Will Take Your Website to The Next Level 🚀
There are over 97 Web APIs, and you’re only using 5% of them. Let’s unlock that other 95 percent! After taking a long stroll down the dark realms of the spec, I realized that there were so many technologies out there being left-out.