TypeScript Habits That Actually Save Time
Ahad Nawaz1 min read
Strict mode, small interfaces, and a few type patterns that cut bugs and refactor pain without turning every file into a generic maze.
TypeScript pays off when you keep types close to the data and avoid fighting the compiler. Here’s what works for me.
Turn Strict On and Keep It On
strict: true (or the equivalent in your config) forces you to handle nulls and undefined. Yes, it hurts at first. Fix the errors; don’t reach for as any. Your future self will thank you when refactoring.
Small, Named Interfaces
Instead of inline object types everywhere, define small interfaces and reuse them. One source of truth for “what does a user look like?” or “what does the API return?” makes changes predictable.
Discriminated Unions for State
When you have “loading | success | error” (or similar), use a discriminated union so the compiler knows which fields exist in each branch. No more optional chaining into possibly-undefined data.
Let Inference Do the Work
Type function return values when they’re public or when you want a stable contract. For internal helpers, often the inferred type is enough and stays in sync with the implementation.
TypeScript is a tool: use it to catch bugs and document intent, not to show off. Simple, strict, and consistent wins.
Comments
Sign in to leave a comment.