Convert JavaScript code to TypeScript with proper type definitions, strict mode compliance, and incremental adoption strategy.
Fill in this template
The {{ }} placeholders are variables — drop in your own details, then copy the ready-to-use prompt. (Or copy the template as-is and let your AI assistant fill them in.)
0/2 filled
Preview filled prompt
You are a TypeScript migration expert.
## JavaScript Code to Migrate
```javascript
{{CODE}}
```
## Strictness Level
{{STRICTNESS_LEVEL}}
(Options: basic = minimal types, moderate = good coverage, strict = strict mode compliant, maximum = strict + no-any)
## Migration Instructions
### 1. Type Analysis
Before writing any TypeScript, analyze the code:
- What are the implicit types of all variables?
- What are the function signatures (params and return types)?
- What are the object shapes used throughout?
- Are there any union types, discriminated unions, or generics needed?
- Are there runtime type checks that hint at expected types?
### 2. Type Definitions
Create type definitions for:
- **Interfaces**: For object shapes used as function params or return values
- **Type aliases**: For unions, intersections, and utility types
- **Enums or const objects**: For string literal unions
- **Generics**: Where type parameters improve reusability
- **Utility types**: Use Pick, Omit, Partial, Required, Record where appropriate
### 3. Migration Rules by Strictness Level
#### Basic
- Rename .js to .ts
- Add types where TypeScript complains
- Use 'any' sparingly for complex types to unblock
#### Moderate
- Full function signatures (params + return types)
- Interface definitions for all object shapes
- No implicit 'any' in function parameters
- Proper null/undefined handling
#### Strict (tsconfig: strict = true)
- No 'any' except in truly dynamic scenarios (with comment explaining why)
- Strict null checks (no non-null assertions unless justified)
- No implicit returns
- Proper error typing in catch blocks
- Discriminated unions for variant types
#### Maximum (strict + custom rules)
- Zero 'any' — use 'unknown' + type guards instead
- Branded types for IDs and sensitive values
- Readonly where mutation isn't needed
- Const assertions for literal types
- Exhaustive switch/if checks with 'never'
### 4. Common Patterns to Transform
- `module.exports` -> `export` / `export default`
- `require()` -> `import`
- Dynamic property access -> proper index signatures or Maps
- `arguments` -> rest parameters
- `this` binding -> arrow functions or explicit typing
- `.bind()` patterns -> arrow functions
- `typeof x === 'object'` -> proper type guards
## Output Format
1. **Type Definitions File** (.d.ts or inline): All interfaces and types
2. **Migrated Code**: Complete TypeScript version
3. **tsconfig.json Changes**: Any config needed for the strictness level
4. **Migration Notes**: Decisions made and trade-offs
5. **Remaining TODOs**: Types that need real-world data to finalize (marked with // TODO)Tags
migrationtypescriptjavascripttype-safety
Explore more prompts and rules
BeforeMerge offers hundreds of AI prompts, code review rules, guides, and detection patterns to help your team ship better code.