Useful Hook Patterns for Real Projects
Explore practical Claude Code hook patterns: automatic TypeScript type checking after edits, query duplication prevention using a second Claude instance, and additional hook event types.
Now that you understand how hooks work, let us explore patterns that solve real problems in professional development workflows. These hooks address common weaknesses in AI-assisted development — from missed type errors to accidental code duplication — and can dramatically improve the quality of AI-generated code on larger projects.
Pattern 1: Automatic TypeScript Type Checking
One of the most common issues with AI-assisted coding: when Claude modifies a function signature, it often misses updating all the call sites throughout your project. The function definition is correct, but callers in other files still use the old signature — creating type errors that Claude does not immediately catch.
The solution is a PostToolUse hook that runs the TypeScript compiler after every file edit:
- Triggers after Write, Edit, or MultiEdit operations
- Runs tsc --noEmit to check for type errors across the entire project
- Captures any errors and feeds them back to Claude immediately
- Claude then fixes the issues in other files without being asked
Works with any typed language
This pattern works for any language with a static type checker. For Python, use mypy or pyright. For Go, use go vet. For untyped languages, you can run automated tests instead to catch regressions.
Pattern 2: Preventing Code Duplication
In larger projects with many database queries or utility functions, Claude sometimes writes new code instead of reusing existing functionality. This is especially problematic during complex, multi-step tasks where the database query is just one small component — Claude focuses on the bigger picture and misses the existing implementation.
The duplication prevention hook uses a powerful approach: it launches a second Claude Code instance to review changes:
- Triggers when Claude modifies files in a watched directory (e.g., ./queries)
- Launches a separate Claude Code instance programmatically via the SDK
- The second instance reviews the changes and checks for similar existing code
- If duplicates are found, it feeds feedback to the original Claude instance
- Claude then removes the duplicate and uses the existing implementation
Performance trade-off
The duplication hook launches a separate Claude instance for each review, which adds time and API usage. Only monitor critical directories where consistency matters most — high-traffic database query folders, shared utility libraries, or API route handlers.
Beyond PreToolUse and PostToolUse
Claude Code offers several additional hook event types beyond the two we have focused on:
- Notification — triggers when Claude needs permission or after 60 seconds of idle time
- Stop — runs when Claude finishes responding
- SubagentStop — runs when a subagent (displayed as "Task" in the UI) completes
- PreCompact — runs before a manual or automatic compact operation
- UserPromptSubmit — runs when you submit a prompt, before Claude processes it
- SessionStart — runs when starting or resuming a session
- SessionEnd — runs when a session ends
Debugging Hook Input with jq
Since the JSON input structure varies by hook type and tool matcher, it can be challenging to know what data your command will receive. A helpful debugging technique is to create a logging hook that writes the input to a file:
1"PostToolUse": [2 {3 "matcher": "*",4 "hooks": [5 {6 "type": "command",7 "command": "jq . > hook-debug-log.json"8 }9 ]10 }11]This writes the full input payload to hook-debug-log.json for every tool use, letting you inspect exactly what data your production hook will receive.
Key Takeaways
- 01A TypeScript type-checking hook catches missed call-site updates after Claude edits function signatures.
- 02A duplication prevention hook uses a second Claude instance to review changes for existing code.
- 03Beyond PreToolUse/PostToolUse, Claude Code offers hooks for notifications, stops, compacts, prompts, and sessions.
- 04Use a jq logging hook to inspect the exact JSON your command receives — the structure varies by hook type.
- 05Balance automation benefits against performance costs — monitor only high-value directories.