JavaScript to TypeScript Converter

Paste JavaScript code and get a TypeScript version with type annotations added. Handles function parameters, variable types, var replacement, and require() imports. Best-effort — complex logic needs manual review.

Best-effort conversion. This tool applies common transformations using pattern matching. Complex dynamic types, overloads, conditional types, and library-specific APIs require manual TypeScript authoring. Always review the output before using it in production.
JavaScript Input Paste .js code
TypeScript Output .ts ready
🔎
Type Inference
Infers string, number, boolean, unknown[], and Record from initializer values
🔨
Function Params
Adds : unknown to untyped function parameters in both declaration and arrow function syntax
🔄
require() to import
Converts CommonJS require() calls to ES module import statements
📋
Download .ts
Save the converted file directly as a .ts file ready to drop into your TypeScript project

Conversion Examples

JavaScript
var name = 'Alice'; const age = 30; const active = true; const scores = []; const user = {};
TypeScript
let name: string = 'Alice'; const age: number = 30; const active: boolean = true; const scores: unknown[] = []; const user: Record<string, unknown> = {};
JavaScript
function greet(name, age) { return 'Hello ' + name; } const add = (a, b) => a + b; const fetch = require('node-fetch');
TypeScript
function greet(name: unknown, age: unknown) { return 'Hello ' + name; } const add = (a: unknown, b: unknown) => a + b; import fetch from 'node-fetch';

Migrating JavaScript to TypeScript

TypeScript is a superset of JavaScript, which means all valid JavaScript is also valid TypeScript. The migration process is not about rewriting code — it is about adding type information so the TypeScript compiler can catch type errors at build time rather than at runtime. The most common first steps are renaming .js files to .ts, replacing var with let or const, converting CommonJS require() calls to ES module imports, and adding type annotations to function parameters that TypeScript cannot infer.

TypeScript can infer types from variable initializers: if you write const count = 0, TypeScript knows count is a number without an explicit annotation. The places where manual annotations are most needed are function parameters (TypeScript cannot infer what callers will pass), empty array and object literals (TypeScript infers never[] and {} without guidance), and function return types when the return value is complex.

For large codebases, TypeScript's allowJs and checkJs compiler options let you migrate incrementally — JavaScript files continue to work alongside TypeScript files, and you can convert one module at a time. Setting "strict": true in tsconfig.json enables the full set of type checks that catch the most bugs. This tool handles the mechanical transformations that are the same in every migration — the domain-specific type modeling is where human judgment is always required.

What This Converter Handles

  • var → let: Replaces all var declarations with let. You may want to change some of these to const where the variable is never reassigned.
  • Type inference from initializers: String literals → string, number literals → number, booleans → boolean, []unknown[], {}Record<string, unknown>, nullnull.
  • Function parameters: Adds : unknown to unannotated parameters in function declarations and arrow functions. Parameters already annotated are left unchanged.
  • require() → import: Converts const x = require('mod') to import x from 'mod' and const {a, b} = require('mod') to import {a, b} from 'mod'.
  • Not handled: Generic types, union types, interfaces, type aliases, class property types, JSX props, overloads, conditional types, mapped types, and library-specific types. These require manual authoring.