Last updated
JavaScript to TypeScript Converter
Convert JavaScript code to TypeScript automatically. This tool adds type annotations, converts var to let/const, adds interfaces for objects, and applies TypeScript best practices to your JavaScript code.
Conversion Features
- Type Inference: Automatically infers types from values
- Interface Generation: Creates interfaces for object parameters
- Modern Syntax: Converts var to let/const
- Function Types: Adds parameter and return types
- Array Types: Converts to typed arrays
- Null Safety: Adds optional chaining where appropriate
Migration Tips
1. Start with .ts Extension
Rename .js files to .ts and fix errors gradually
2. Enable Strict Mode
Use "strict": true in tsconfig.json for better type safety
3. Add Types Incrementally
Start with any types, then refine to specific types
4. Use Type Definitions
Install @types packages for third-party libraries
Migration Strategy
- Rename .js files to .ts one at a time — start with utility functions and types
- Use
anyas a temporary escape hatch — replace with proper types incrementally - Install type definitions:
npm install -D @types/node @types/express @types/react - Run
npx tsc --noEmitto check for type errors without compiling - Enable
strict: truein tsconfig.json once most files are converted - Use
// @ts-ignoresparingly for complex cases you'll fix later
Paste your JavaScript to get TypeScript with type annotations. All conversion happens in your browser — your code is never sent to any server.
Common Conversions
Variables
// JavaScript
var name = "John";
// TypeScript
const name: string = "John";
Functions
// JavaScript
function add(a, b) {
return a + b;
}
// TypeScript
function add(a: number, b: number): number {
return a + b;
}
Objects
// JavaScript
const user = {
name: "John",
age: 30
};
// TypeScript
interface User {
name: string;
age: number;
}
const user: User = {
name: "John",
age: 30
};
Frequently Asked Questions
Q: Will my JavaScript code break in TypeScript?
A: No, all valid JavaScript is valid TypeScript. You can gradually add types without breaking existing code.
Q: Do I need to convert everything at once?
A: No, you can migrate file by file. TypeScript supports mixed JS/TS projects.
Q: Is this tool free?
A: Yes, completely free with no signup required. All processing happens in your browser.
Examples
Example 1: Simple Function Conversion
JavaScript:
function add(a, b) {
return a + b;
}
function formatCurrency(amount, currency) {
return `${currency}${amount.toFixed(2)}`;
}
TypeScript:
function add(a: number, b: number): number {
return a + b;
}
function formatCurrency(amount: number, currency: string): string {
return `${currency}${amount.toFixed(2)}`;
}
Example 2: Object and Interface Generation
JavaScript:
function processOrder(order) {
const total = order.items.reduce((sum, item) => {
return sum + item.price * item.quantity;
}, 0);
return {
orderId: order.id,
total,
status: 'processed',
processedAt: new Date().toISOString()
};
}
TypeScript:
interface OrderItem {
price: number;
quantity: number;
}
interface Order {
id: string;
items: OrderItem[];
}
interface ProcessedOrder {
orderId: string;
total: number;
status: string;
processedAt: string;
}
function processOrder(order: Order): ProcessedOrder {
const total = order.items.reduce((sum: number, item: OrderItem) => {
return sum + item.price * item.quantity;
}, 0);
return {
orderId: order.id,
total,
status: 'processed',
processedAt: new Date().toISOString()
};
}
Example 3: Union Types and Optional Properties
JavaScript:
function formatValue(value) {
if (typeof value === 'string') {
return value.toUpperCase();
}
if (typeof value === 'number') {
return value.toFixed(2);
}
return String(value);
}
function createUser(name, email, role) {
return {
name,
email,
role: role || 'user',
createdAt: Date.now()
};
}
TypeScript:
function formatValue(value: string | number | boolean): string {
if (typeof value === 'string') {
return value.toUpperCase();
}
if (typeof value === 'number') {
return value.toFixed(2);
}
return String(value);
}
type UserRole = 'admin' | 'user' | 'moderator';
interface NewUser {
name: string;
email: string;
role: UserRole;
createdAt: number;
}
function createUser(name: string, email: string, role?: UserRole): NewUser {
return {
name,
email,
role: role || 'user',
createdAt: Date.now()
};
}