Skip to main content
coding intermediate

Convert JavaScript to TypeScript

Transform your JavaScript code to TypeScript with proper types, interfaces, and best practices. Get clean, type-safe code instantly.

Works with: chatgptclaudegemini

Prompt Template

You are an expert TypeScript developer tasked with converting JavaScript code to TypeScript. Please convert the following JavaScript code to TypeScript with proper typing, following these requirements: **Conversion Requirements:** 1. Add appropriate type annotations for all variables, function parameters, and return types 2. Create interfaces or types for complex objects and data structures 3. Use proper TypeScript generics where applicable 4. Handle any implicit 'any' types with explicit typing 5. Add JSDoc comments for complex functions if not already present 6. Ensure strict TypeScript compliance (no 'any' types unless absolutely necessary) 7. Use modern TypeScript features and best practices 8. Maintain the original functionality while improving type safety **JavaScript Code to Convert:** ```javascript [JAVASCRIPT_CODE] ``` **Additional Context:** [CONTEXT] **Target TypeScript Version:** [TYPESCRIPT_VERSION] Please provide: 1. The converted TypeScript code with proper formatting 2. Any additional type definitions or interfaces needed 3. Brief explanation of key typing decisions made 4. Any potential runtime behavior changes or considerations 5. Suggestions for further improvements if applicable Ensure the converted code is production-ready and follows TypeScript best practices.

Variables to Customize

[JAVASCRIPT_CODE]

The JavaScript code you want to convert to TypeScript

Example: function fetchUser(id) { return fetch(`/api/users/${id}`) .then(response => response.json()) .then(data => { return { id: data.id, name: data.name, email: data.email, createdAt: new Date(data.created_at) }; }); }

[CONTEXT]

Additional context about the code's purpose, dependencies, or usage

Example: This function fetches user data from a REST API and transforms the response. It's used in a React application.

[TYPESCRIPT_VERSION]

The target TypeScript version for compatibility

Example: 5.0

Example Output

Here's the converted TypeScript code: ```typescript interface ApiUserResponse { id: number; name: string; email: string; created_at: string; } interface User { id: number; name: string; email: string; createdAt: Date; } async function fetchUser(id: number): Promise<User> { const response = await fetch(`/api/users/${id}`); if (!response.ok) { throw new Error(`Failed to fetch user: ${response.statusText}`); } const data: ApiUserResponse = await response.json(); return { id: data.id, name: data.name, email: data.email, createdAt: new Date(data.created_at) }; } ``` **Key Typing Decisions:** 1. Created `ApiUserResponse` interface for the raw API response 2. Created `User` interface for the transformed user object 3. Added proper error handling with type-safe response checking 4. Used `async/await` for better readability and error handling 5. Specified `number` type for user ID parameter **Considerations:** Converted to async/await pattern for better error handling, though the original Promise chain functionality is preserved.

Pro Tips for Best Results

  • Provide complete code context including imports and related functions for better type inference
  • Specify if you're using any particular frameworks (React, Node.js, etc.) for more accurate typing
  • Include example data structures or API responses to help generate more precise interfaces
  • Mention your tsconfig.json settings or strict mode preferences for compatibility
  • Ask for gradual migration strategies if converting large codebases incrementally

Tags

Want 500+ Expert Prompts?

Get the Premium Prompt Pack — organized, tested, and ready to use.

Get it for $29

Related Prompts You Might Like