convert_to_typescript – Modern TypeScript Migration

Intelligent JavaScript to TypeScript conversion with comprehensive type annotations and modern development patterns

Use local-llm:convert_to_typescript to modernise JavaScript codebases with proper TypeScript types and enhanced developer experience.

Perfect For

Legacy Codebase Modernisation
Systematic conversion of existing JavaScript projects to TypeScript with preserved functionality and enhanced type safety.

Team Productivity Enhancement
Improve development experience with IntelliSense, compile-time error detection, and better refactoring capabilities.

Enterprise Code Quality
Establish type safety standards for large codebases with comprehensive interface definitions and strict typing.

API Contract Definition
Create robust API interfaces with proper request/response typing and validation support.

Library Development
Professional TypeScript libraries with complete type definitions for better developer adoption and usage.

Quick Start

local-llm:convert_to_typescript with:
- filePath: "C:/src/userService.js"
- strict: true
- addTypeGuards: true

Batch conversion with specific target:

local-llm:convert_to_typescript with:
- projectPath: "C:/legacy-project/src"
- target: "ES2022"
- useInterfaces: true
- analysisDepth: "comprehensive"

Conversion Output

Type System Integration

  • Complete Type Annotations: Function parameters, return types, and variable declarations with precise typing
  • Interface Definitions: Well-structured interfaces for complex objects and API responses
  • Generic Type Definitions: Reusable generic types for flexible, type-safe code patterns
  • Union and Intersection Types: Advanced type compositions for complex business logic

Modern Language Features

  • ES6+ Syntax Upgrades: Modern import/export, arrow functions, and destructuring patterns
  • Type Guards and Assertions: Runtime type checking functions for safe type narrowing
  • Enum Definitions: Strongly-typed enums for constants and configuration values
  • Async/Await Typing: Proper Promise typing with comprehensive error handling

Development Experience

  • TSDoc Comments: Enhanced documentation with type information and examples
  • Configuration Files: Optimised tsconfig.json with appropriate compiler options
  • IDE Integration: IntelliSense-friendly type definitions for better developer productivity

Conversion Example

JavaScript Input

function createUser(userData, options) {
  const config = {
    validateEmail: true,
    sendNotification: false,
    ...options
  };
  
  if (config.validateEmail && !isValidEmail(userData.email)) {
    throw new Error('Invalid email');
  }
  
  return {
    id: generateId(),
    ...userData,
    createdAt: new Date(),
    preferences: getDefaultPreferences()
  };
}

TypeScript Output

interface UserData {
  email: string;
  name: string;
  avatar?: string;
}

interface CreateUserOptions {
  validateEmail?: boolean;
  sendNotification?: boolean;
}

interface User extends UserData {
  id: string;
  createdAt: Date;
  preferences: UserPreferences;
}

function createUser(userData: UserData, options: CreateUserOptions = {}): User {
  const config: Required = {
    validateEmail: true,
    sendNotification: false,
    ...options
  };
  
  if (config.validateEmail && !isValidEmail(userData.email)) {
    throw new Error('Invalid email');
  }
  
  return {
    id: generateId(),
    ...userData,
    createdAt: new Date(),
    preferences: getDefaultPreferences()
  };
}

// Type guard for runtime validation
function isValidUserData(data: unknown): data is UserData {
  return (
    typeof data === 'object' &&
    data !== null &&
    typeof (data as UserData).email === 'string' &&
    typeof (data as UserData).name === 'string'
  );
}

Parameters

ParameterTypeDescriptionExample
filePathstringJavaScript file to convert“C:/src/service.js”
codestringJavaScript code snippet for conversion“function example() {…}”
projectPathstringRoot directory for batch conversion“C:/project/src”
strictbooleanEnable strict TypeScript modetrue
targetenumTypeScript compilation target“ES2020” | “ES2022” | “ESNext”
addTypeGuardsbooleanGenerate runtime type checkingtrue
useInterfacesbooleanPrefer interfaces over type aliasestrue

TypeScript Configuration Options

  • strict: true – Enable all strict type checking options for maximum type safety
  • target: “ES2022” – Modern JavaScript features with excellent browser support
  • addTypeGuards: true – Generate runtime validation functions for type safety
  • useInterfaces: true – Prefer interfaces for better extensibility and declaration merging

Advanced Configuration

Framework-Specific Conversion: Optimised patterns for different technology stacks.

// React component conversion
local-llm:convert_to_typescript with:
- filePath: "C:/components/UserCard.jsx"
- strict: true
- target: "ES2022"
- context: {"framework": "React", "hooks": true}

// Node.js API conversion
local-llm:convert_to_typescript with:
- projectPath: "C:/api/routes"
- strict: true
- addTypeGuards: true
- context: {"framework": "Node.js", "express": true}

Migration Strategy Workflow:

  1. Start with utility functions and data models
  2. Convert core business logic with comprehensive typing
  3. Update API layers with request/response interfaces
  4. Convert UI components with proper prop typing
  5. Generate comprehensive tests with generate_unit_tests

Generated Features

Type Safety Enhancements:

  • Null Safety: Strict null checks and optional chaining implementations
  • Runtime Validation: Type guard functions for API boundary validation
  • Generic Constraints: Properly constrained generic types for reusable components
  • Discriminated Unions: Type-safe variants and pattern matching

Modern Development Patterns:

  • Utility Types: Partial, Pick, Omit, and custom utility types for data transformation
  • Template Literal Types: Type-safe string manipulation and validation
  • Conditional Types: Advanced type logic for complex business rules

Pro Tips

Incremental Adoption: Start conversion with leaf modules and work inward to avoid complex dependency issues during migration.

Type Definition Quality: Generated interfaces include proper documentation and examples for better developer understanding.

Build Integration: Conversion includes tsconfig.json optimised for your project’s target environment and requirements.

Related Functions

  • suggest_refactoring – Code quality improvements that work well with TypeScript conversion
  • generate_unit_tests – TypeScript-aware test generation with proper type checking
  • analyze_single_file – Pre-conversion analysis to understand code structure and complexity
  • generate_documentation – Updated documentation reflecting TypeScript interfaces and types