Skip to main content

A few TypeScript habits that actually saved me time

No grand theory, just the handful of things I do in every project now because I got burned without them.

Vishwajit BhosleSoftware developer, Pune 3 min read
On this page

I've read a lot of TypeScript advice that's technically correct and practically useless, like "use strict mode" or "avoid any". Fine. Here are the habits I picked up from actual mistakes, which is a different list.

Parse at the edge, trust inside

Data coming from outside your app, like an API response, a form, or something in local storage, is not the type you wrote down. TypeScript can't know that. If you write const user = (await res.json()) as User, you've just told the compiler to stop worrying, and it will, loudly, until the day the API changes a field.

I now validate anything that crosses a boundary with a schema library (Zod, mostly) and let the types come from the schema. After that point, inside the app, I trust the types fully. This has caught more real bugs for me than any other single habit, and most of them were the boring kind: a field that was sometimes null, a number that arrived as a string.

Make impossible states impossible

I used to model a request like this: { loading: boolean; error: string | null; data: Thing | null }. It looks reasonable. It also allows loading: true together with an error and data, which means nothing, and every component then has to guess which combination it's looking at.

A union fixes it. A request is idle, or loading, or failed with an error, or done with data, and each of those carries only what makes sense for it. The compiler then forces you to handle every case, and the "how did we get here" bugs go away. It's a bit more typing and it's worth it every single time.

Don't over-type the clever bits

The opposite mistake is also real. I've written types so elaborate that nobody, including me a week later, could read them. Conditional types stacked three deep, generics with five parameters, to save writing two small functions.

My rule now is that if a type takes me more than a few minutes to understand when I come back to it, it's too clever, and I should either simplify it or write the code the boring way. Types are there to help the next person read the code. When they stop doing that, they're a liability.

Let the compiler tell you what to change

When I'm refactoring, I change a type first and then follow the red squiggles. It's a surprisingly good way to find every place that needs updating, much better than searching for strings. Renaming a field, adding a case to a union, changing a function's argument: the compiler hands me a to-do list, and I work through it until it's quiet.

On a bigger codebase this has saved me from the kind of "I think I got everything" feeling that always turns out to be wrong.

Keep any out, but be honest when you use it

Sometimes you do need an escape hatch, like a messy third-party library or a quick prototype. That's okay. What I try to do is leave a short comment saying why, so the next person knows it was a decision and not an accident. I've found unknown is nearly always the better choice, because it makes you check before you use the value, which is what you wanted anyway.

None of this is deep. It's mostly about making mistakes show up while I'm typing, not in production. That's the whole point of the thing, I think, and I forget it sometimes when I get too attached to clever types.

Enjoyed this? Pass it on

Written by Vishwajit Bhosle

I build and lead web and mobile products from Pune, and write about what I run into along the way. If something here helped, or you have a project in mind, say hello.

Follow along

New posts show up in any feed reader the moment they are published: no account, no email, no tracking.

Open in Feedly

Comments

Anyone can comment, no sign-up. Upvote what helped, reply to anyone. I read all of them and reply where I can.

Loading comments…