TypeScript Best Practices
TypeScript Best Practices: Habits That Keep Codebases Clean
Good TypeScript makes bugs obvious and refactoring fearless without burying code under complex type gymnastics. Following a small set of team habits keeps compile times fast and types readable for every developer on the project.
The highest-impact step is turning on "strict": true and "noUncheckedIndexedAccess": true in tsconfig.json from day one.
Core Rules for Production TypeScript
- Ban implicit and lazy
any: Useunknownfor untrusted data (API JSON,catcherrors) and narrow with type guards or schema validators like Zod. - Use
import typefor type-only imports: Writingimport type { User } from "./types"guarantees zero runtime bundle imports and prevents circular runtime dependency issues. - Prefer
as constunions over numeric enums: Literal unions ("idle" | "loading" | "error") are lighter, work with Node type stripping, and serialize cleanly. - Derive DTOs with utility types: Build
CreateUser = Omit<User, "id">andUpdateUser = Partial<CreateUser>instead of copy-pasting properties across five files.
// Good: derive types from one source of truth + narrow unknown input
interface Order {
readonly id: string;
amount: number;
status: "pending" | "paid";
}
type OrderDraft = Omit<Order, "id">;
function isOrderDraft(raw: unknown): raw is OrderDraft {
return (
typeof raw === "object" &&
raw !== null &&
typeof (raw as OrderDraft).amount === "number" &&
((raw as OrderDraft).status === "pending" || (raw as OrderDraft).status === "paid")
);
}
const sample: unknown = { amount: 49.5, status: "paid" };
if (isOrderDraft(sample)) {
console.log(sample.amount, sample.status);
}
Common Mistakes to Avoid
- Silencing errors with
// @ts-ignoreoras any: Always prefer// @ts-expect-error(with a comment), which alerts you when the underlying error disappears. - Writing overly clever 10-line conditional types in app code: If a teammate cannot read a type signature in five seconds, simplify it with concrete interfaces or overloads.
- Trusting API responses with
as MyType: Validate external inputs at boundaries once, then pass strongly typed objects everywhere internally.
Frequently Asked Questions
Should I enable "noUncheckedIndexedAccess": true?
Yes. It adds | undefined when indexing arrays (arr[0]) or records (map[key]), catching out-of-bounds bugs before runtime—just enable it early or fix existing files incrementally.
What is the difference between // @ts-ignore and // @ts-expect-error?
// @ts-expect-error fails compilation if the line below stops having an error, preventing stale suppression comments from piling up.
How do I keep tsc fast in a large monorepo?
Use TypeScript project references ("composite": true, tsc -b), keep "skipLibCheck": true, and avoid massive deeply nested conditional/mapped types.
Unlock Your
Full Potential.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Try it Yourself
Experiment with the code from this lesson in our interactive playground.