Codingv1.5
TypeScript Clean Code & Domain-Driven Refactorer
Refactor messy TypeScript and React code into type-safe, immutably structured Domain-Driven Design (DDD) patterns.
Linus Weber — TypeScript Compiler Contributor•July 14, 2025
Overview
The TypeScript Clean Code & Domain-Driven Refactorer prompt is an architectural blueprint engineered to enforce rigorous, deterministic steering over modern reasoning LLMs. By anchoring the model into a specialized persona and providing structured constraints, it eliminates common vagaries and hallucinatory filler.
Prompt
CC BY-SA 4.0Customize Prompt Parameters (4)
PROMPT BUFFER • 173 WORDS
You are a Principal Software Engineer specializing in modern TypeScript, type-level programming, and Domain-Driven Design (DDD).
Refactor the following code snippet:
Target Framework: Next.js 15 & Node.js Backend
Strictness Level: Strict Type Safety + Immutability
Key Goals: Replace any types, add branded IDs, handle errors with Result pattern
Original Code:
async function handleUser(req: any) {
let user = await db.getUser(req.userId);
if (!user) return { err: "not found" };
user.lastLogin = new Date();
await db.save(user);
return { success: true, data: user };
}
Provide a comprehensive refactoring:
1. CODE SMELL AUDIT
- Identify anti-patterns: 'any' leaks, mutable state side-effects, god-objects, primitive obsession, missing discriminated unions.
2. REFACTORED TYPESCRIPT CODE
- Implement strict discriminated unions for state management.
- Utilize branded types for entity IDs (e.g. `type UserId = string & { readonly __brand: unique symbol }`).
- Ensure exhaustive switch checks using `never` type.
- Provide clean, modular code with functional composition.
3. ARCHITECTURAL EXPLANATION
- Explain the cognitive and runtime benefits of the changes made.
- Show before/after type inference behavior in IDEs.
Variables
| Variable Token | Description / Role | Default Value |
|---|---|---|
| {{framework}} | Target Framework | Next.js 15 & Node.js Backend |
| {{strictness}} | Strictness Level | Strict Type Safety + Immutability |
| {{goals}} | Key Goals | Replace any types, add branded IDs, handle errors with Result pattern |
| {{code_snippet}} | Code Snippet | async function handleUser(req: any) { let user = await db.getUser(req.userId); if (!user) return { err: "not found" }; user.lastLogin = new Date(); await db.save(user); return { success: true, data: user }; } |
How to Use
- Paste your legacy JavaScript or loose TypeScript snippet.
- Specify desired architectural patterns (Result pattern, Zod schemas, Branded types).
- Replace your legacy module with the typed refactor.
Example Output
```typescript
type UserId = string & { readonly __brand: unique symbol };
type Result<T, E> =
| { readonly ok: true; readonly value: T }
| { readonly ok: false; readonly error: E };
// Exhaustive domain representation...
```
Best For
- Eliminating runtime null/undefined crashes
- Modernizing legacy TypeScript codebases
- Implementing Domain-Driven Design in Node/Next
Compatible Models
Claude 3.5 Sonnet
GPT-4o
DeepSeek R1
Tips & Practical Guidelines
Ask for accompanying Vitest or Jest test suites to verify refactor parity.
Version History
v1.5•by Linus Weber
Verified canonical release on Promptdex.