All Things Typescript

Share this post

Tracking Type Coverage in Typescript - Why? - All Things Typescript Newsletter - Issue #18

www.allthingstypescript.dev

Tracking Type Coverage in Typescript - Why? - All Things Typescript Newsletter - Issue #18

Maina Wycliffe
Aug 1, 2022
1
Share this post

Tracking Type Coverage in Typescript - Why? - All Things Typescript Newsletter - Issue #18

www.allthingstypescript.dev

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.

Thanks for reading All Things Typescript! Subscribe for free to receive new posts and support my work.

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.

Share


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.

Join Discord Server


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…

dev.to

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.

javascript.plainenglish.io

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.

medium.com

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.

partytown.builder.io

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.

www.danielsieger.com

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.

www.freecodecamp.org

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

www.freecodecamp.org

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.

dev.to


Tweets

Twitter avatar for @TitianCernicova
Titian Cernicova Dragomir 💙💛 @TitianCernicova
So it just hit me that since #TypeScript can now narrow parameters that were destructured from a discriminated union this works, and I'm not sure if I love it or hate it 😅: typescriptlang.org/play?ts=4.7.4#…
Image
11:05 AM ∙ Jul 27, 2022
19Likes1Retweet
Twitter avatar for @mwycliffe_dev
Maina Wycliffe @mwycliffe_dev
One thing I keep hearing about developers' point about #Typescript is that you end up writing more code (Type info), but I think this is a positive thing. Yes, I write more code, but this way, I get to express my intention more clearly and end up with more concise code. #webdev
7:38 AM ∙ Aug 1, 2022
9Likes3Retweets
Twitter avatar for @mattpocockuk
Matt Pocock @mattpocockuk
More builder pattern fun. Building a typesafe database seeder for a test suite.
1:54 PM ∙ Jul 19, 2022
370Likes27Retweets
Twitter avatar for @crutchcorn
Corbin Crutchley 🥑 @crutchcorn
You have a junior dev on your team who's taken the initiative to refactor some existing code and make it sleeker and easier to read. But oh no, their migration broke the functionality! 😱 Explain why the code broke to the Junior engineer - what's different in this image?
"before" and "after" code samples live here: https://gist.github.com/crutchcorn/1b69259a35f7618bf73781967ebd10d6
9:07 AM ∙ Jul 18, 2022
18Likes1Retweet
Twitter avatar for @mwycliffe_dev
Maina Wycliffe @mwycliffe_dev
I wrote this thread in the hope of kickstarting a discussion around taking credit for your own achievements, and now I want to hear what people think newsletter.pragmaticdevx.dev/p/taking-credi… #100daysofcode #programming #devx
newsletter.pragmaticdevx.devTaking credit for your own achievementsThe last few weeks have been a sort of Journey for me; I am in the middle of Job hunting and deciding on what my future looks like - career-wise…
5:15 PM ∙ Jul 27, 2022
6Likes4Retweets

Thanks for reading All Things Typescript! Subscribe for free to receive new posts and support my work.

Share this post

Tracking Type Coverage in Typescript - Why? - All Things Typescript Newsletter - Issue #18

www.allthingstypescript.dev
Previous
Next
Comments
TopNewCommunity

No posts

Ready for more?

© 2023 Maina Wycliffe
Privacy ∙ Terms ∙ Collection notice
Start WritingGet the app
Substack is the home for great writing