JavaScript Input
TypeScript Output

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

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

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()
  };
}

Frequently Asked Questions

Paste your JavaScript code into the converter, and it will automatically add TypeScript type annotations, convert var to let/const, add interfaces for objects, and apply TypeScript best practices. The conversion is instant and happens in your browser.

Yes, all valid JavaScript is valid TypeScript. TypeScript is a superset of JavaScript, meaning you can gradually add types to existing JavaScript code.