January 23, 2024

Type TODO

edit

With tongue firmly in cheek, I tweeted the following:

The response the post received was a lot larger than I was expecting, so I thought I’d take a moment to explain why this is both a tip and a joke at the same time.

Tip: Sometimes any is ok

If you’ve written TypeScript for long enough, then I am willing to bet any has come to your rescue a time or two.

Any app of reasonable complexity, especially a codebase that has some age, perhaps still has some JavaScript, will likely have nasty parts that are difficult to type correctly.

Add to this any time pressure or consideration between value created versus the time it’s taking to do so, and you’ll find yourself in a situation where it is just not worth it to solve that hairy type problem right now. That’s where the TODO type comes in handy.

type TODO = any

The scenario that prompted me to write the tweet was converting a JavaScript file to TypeScript. I had a value I couldn’t get the types right for in the first few minutes. It was a file that was already JavaScript, and thus inherently not type-safe, and it was a file that hadn’t been modified in years. Given these factors, using an any was appropriate. Making it a TODO provides some signal that perhaps a time may come where it can be updated and improved.

Don’t forget, you don’t have to name it TODO either. You can name it something more specific. Perhaps you prefix a typename with UNSAFE_ or something similar to signal that you’ve opted out of type safety.

Joke: You shouldn’t use TODO excessively

While there are appropriate times to use the TODO type, it definitely should not be abused. If many parts, or particularly important parts, are littered with TODO or any types, then you may want to consider if it’s possible to devote more time to getting the types correct.

We want to use any or TODO purposefully, as the very rare escape hatch and a signal that the following code is unsafe. Don’t just use them to hide something under the proverbial rug, use them to make it clear that something requires further attention when possible.

Bonus tip

I use a VSCode extension, TODO Highlight, that highlights keywords in your editor. With a few tweaks to the settings, the extension will highlight every use of TODO in your app. That way, you never visually miss where you have them.

Summary

Recognize any or TODO can be useful in a pinch, especially when considering the time/value tradeoff. Do treat them like a real TODO and try and fix them when you have time. Use either type sparingly.

Lastly, keep improving your TypeScript skills and you’ll find you reach for these types less often.


Liked the post?
Give the author a dopamine boost with a few "beard strokes". Click the beard up to 50 times to show your appreciation.
Want to read more?
Newer Post: No Outer margin
Tags
Need help with your software problems?

My team and I are ready to help you. Hire Agathist to build your next great project or to improve one of your existing ones.

Get in touch
Kyle Shevlin's face, which is mostly a beard with eyes

Kyle Shevlin is the founder & lead software engineer of Agathist, a software development firm with a mission to build good software with good people.

Agathist
Good software by good people.
Visit https://agath.ist to learn more
Sign up for my newsletter
Let's chat some more about TypeScript, React, and frontend web development. Unsubscribe at any time.
Logo for Just Enough Functional Programming
Just Enough Functional Programming
Check out my courses!
If you enjoy my posts, you might enjoy my courses, too. Click the button to view the course or go to Courses for more information.