The short version
Keep files under 500 lines, make lint and typecheck run on every commit, ban any, and write your conventions down. Then every 5–10 features, run a consolidation pass.
AI makes you fast, and that speed has a cost: the codebase grows faster than you can review it. Without deliberate hygiene you end up with a project that works today and mystifies you tomorrow.
Four practices keep it healthy: file size, automatic checks, strict TypeScript and written conventions. Then one habit ties them together.
Keep files small
Large files are hard for AI. In a long file, the AI loses track of what’s at the top while it works at the bottom. It writes functions that already exist, duplicates logic, and breaks things outside what it was looking at.
| Lines | Status | Action |
|---|---|---|
| < 200 | Healthy | Nothing to do |
| 200–400 | Watch | Extract if the file does more than one thing |
| 400–500 | Review | Plan the split in the next consolidation pass |
| 500+ | Split now | Split before adding anything else |
What to pull out when a file grows:
| Signal | Action |
|---|---|
| Several components in one file | One file per component |
| The same logic in two or more places | A shared utility module |
| A file fetches data AND renders it | Separate the two |
| Styling classes getting unwieldy | A variants object |
| Types defined inline everywhere | One types module per area |
Ask the AI for a list before it changes anything:
Review the files in [folder]. Flag any over 400 lines,
find duplicate logic, and suggest what to extract.
Don't change anything yet. Give me a list first.
Review the list yourself. Structural decisions belong to people.
Make lint and typecheck automatic
Lint and typecheck aren’t style preferences. They catch real bugs before they run. The trick is to make them run without you remembering:
# Install the tools
npm install -D eslint prettier husky lint-staged
# Run lint-staged before every commit
npx husky init
echo "npx lint-staged" > .husky/pre-commit
Then, in package.json, tell lint-staged to check only the files you changed:
{
"lint-staged": {
"*.{ts,tsx}": ["eslint --fix", "prettier --write"],
"*.{css,json,md}": ["prettier --write"]
}
}
Now nothing gets committed without passing.
Use strict TypeScript
any is a lie you tell TypeScript so it stops complaining. It was complaining for a reason, and now that reason is invisible until it fails at runtime. AI assistants reach for any whenever they’re unsure of a type, which makes it one of the most common ways AI code quietly gets worse.
Start with this in tsconfig.json:
{
"compilerOptions": {
"strict": true,
"noUncheckedIndexedAccess": true
}
}
strict already turns on noImplicitAny and strictNullChecks. Then audit now and then:
Search the codebase for `: any`, `as any` and `@ts-ignore`.
List each one with its file and line. Don't fix anything yet.
The fix is usually unknown with a proper check, or an interface that just needed writing.
Using Supabase?
Regenerate your types after every migration (supabase gen types typescript --local > src/types/supabase.ts). Stale generated types are a common cause of “TypeScript passes, but it fails at runtime”.
Write your conventions down
Your folder structure and naming are a language. If you don’t define it, the AI invents its own dialect, and over time files start contradicting each other. Decide these early:
| Decision | A sensible default |
|---|---|
| Component folders | By kind: ui/, layout/, sections/ |
| Naming | PascalCase for components, kebab-case for files |
| Types | One place, such as src/types/ |
| Utilities | Separate from components, such as src/lib/ |
| Database | A short doc of tables, rules and functions |
Put them in conventions.md and point to it from your context file. If you have a database, a small docs/database/ folder describing the tables and access rules stops the AI guessing at your data every session. The goal is a codebase that looks like one person wrote it.
The consolidation pass
None of the above is one-time setup. Every 5–10 features, set aside 30 minutes:
- Run the file size check and flag anything over 400 lines.
- Run the
anyaudit and fix or properly type each one. - Check new files follow the naming conventions.
- Confirm lint and typecheck pass cleanly.
- Update the context file wherever the AI has drifted.
- Commit the clean-up on its own, so it’s easy to review.
It’s 30 minutes that saves hours of confused debugging later.
Next: Secrets, Git Discipline, and the Mistakes That Kill Projects, or back to the full guide.
