JavaScript to TypeScript Migration
Comprehensive migration guide for converting JavaScript codebases to TypeScript. Contains 34 rules across 10 categories, prioritized by impact to guide automated refactoring and code generation.
When to Apply
Reference these guidelines when:
- Setting up TypeScript in an existing JavaScript project
- Renaming
.jsfiles to.tsand fixing resulting errors - Adding type annotations to functions, classes, and objects
- Enabling and incrementally fixing
strictmode - Converting
require()/module.exportsto ES Moduleimport/export - Typing
Promisereturn values and async error handling - Writing type guards for API responses and external data
- Installing
@typespackages or writing declaration files - Reviewing migrated code for remaining
anytypes or type holes
Rule Categories by Priority
| Priority | Category | Impact | Prefix |
|---|---|---|---|
| 1 | Setup & Configuration | CRITICAL | setup- |
| 2 | Type Annotations | CRITICAL | types- |
| 3 | Conversion Patterns | HIGH | convert- |
| 4 | Strict Mode | HIGH | strict- |
| 5 | Module System | HIGH | modules- |
| 6 | Classes & Interfaces | MEDIUM-HIGH | classes- |
| 7 | Async & Promises | MEDIUM-HIGH | async- |
| 8 | Generics | MEDIUM | generics- |
| 9 | Type Guards & Narrowing | MEDIUM | guards- |
| 10 | Third-party Types | LOW-MEDIUM | libs- |
Quick Reference
1. Setup & Configuration (CRITICAL)
setup-tsconfig-base- Start with a migration-ready tsconfig, not a minimal onesetup-incremental-migration- Migrate file-by-file using allowJs, not all at oncesetup-toolchain- Use tsx, tsc, and proper build scripts consistentlysetup-eslint-typescript- Configure typescript-eslint to catch migration anti-patterns
2. Type Annotations (CRITICAL)
types-avoid-any- Replace any with proper types or unknown at boundariestypes-explicit-return-types- Always annotate function return typestypes-object-shapes- Define interfaces and type aliases for every object shapetypes-array-typing- Type arrays, Maps, Sets, and tuples explicitly
3. Conversion Patterns (HIGH)
convert-require-to-import- Replace CommonJS require with ESM import/exportconvert-prototypes-to-classes- Convert prototype-based code to typed classesconvert-callbacks-to-typed-async- Replace callbacks with typed async/awaitconvert-dynamic-objects- Type dynamic object dictionaries with Record<K,V> or Map
4. Strict Mode (HIGH)
strict-enable-strict- Enable strict mode incrementally per file, not globallystrict-null-checks- Handle null and undefined explicitly with strictNullChecksstrict-no-implicit-any- Fix noImplicitAny errors with real types, not suppressionsstrict-property-initialization- Ensure class properties are initialized properly
5. Module System (HIGH)
modules-esm-imports- Use consistent ESM import/export syntax throughoutmodules-barrel-files- Type barrel index files with explicit named exportsmodules-path-aliases- Configure path aliases to eliminate deep relative imports
6. Classes & Interfaces (MEDIUM-HIGH)
classes-use-interfaces- Define interfaces to decouple consumers from implementationsclasses-access-modifiers- Use private/readonly/protected to enforce encapsulationclasses-abstract-base- Use abstract classes for shared typed behavior
7. Async & Promises (MEDIUM-HIGH)
async-type-promises- Always annotate Promise return types on async functionsasync-error-types- Narrow caught errors before use — don't cast to anyasync-generics-in-async- Write generic async utilities without losing type safety
8. Generics (MEDIUM)
generics-reusable-functions- Make utility functions generic instead of using anygenerics-constrained-generics- Use extends to constrain type parametersgenerics-utility-types- Use Partial, Pick, Omit, Record instead of redefining types
9. Type Guards & Narrowing (MEDIUM)
guards-custom-type-guards- Write type predicates for unknown/union narrowingguards-discriminated-unions- Model multi-state values as discriminated unionsguards-assertion-functions- Use assertion functions to enforce invariants
10. Third-party Types (LOW-MEDIUM)
libs-install-types- Install @types packages for every untyped dependencylibs-declaration-files- Write .d.ts ambient declarations for untyped librarieslibs-module-augmentation- Augment existing types to add runtime-added properties
How to Use
Read individual rule files for detailed explanations and before/after code examples:
rules/setup-tsconfig-base.md
rules/types-avoid-any.md
rules/convert-require-to-import.md
rules/_sections.md
Each rule file contains:
- The rule name and impact level
- Explanation of why it matters during migration
- Incorrect JavaScript or poorly-typed TypeScript example
- Correct TypeScript example with full types
- Reference link to official documentation
Full Compiled Document
For the complete guide with all rules expanded: AGENTS.md
Installation
npx skills add yourusername/agent-js-to-ts-migration