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.


Finished reading?
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.

Kyle Shevlin's face, which is mostly a beard with eyes
Kyle Shevlin is a software engineer who specializes in JavaScript, TypeScript, React and frontend web development.
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 Introduction to State Machines and XState
Introduction to State Machines and XState
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.