How to Convert JavaScript to TypeScript - Step by Step Guide
Renaming a .js file to .ts usually leaves you with a wall of errors: parameters with no type, fields TypeScript has never heard of, and functions that use this without saying what it is. This page fixes exactly those, by adding annotations and never rewriting a line of your logic. It is not a full type inference engine, so expect plenty of any to tighten afterwards.
Paste or upload your JavaScript
Start with any JavaScript you want to move to TypeScript:
Example: JavaScript Input
A function and a class, as plain JavaScript:
function greet(name, greeting = 'Hello') {
return greeting + ', ' + name;
}
class Cart {
constructor(owner) {
this.owner = owner;
this.items = [];
}
}Let the converter add the missing types
The result appears as you type. These are the only kinds of change it makes:
: any, or the type of a literal default (greeting: string = 'Hello'); optional with ? when a call in the file passes fewer argumentsdeclare line for each field set with this.x = .... These lines are type-only and add nothing at run timethis: any on plain functions that use this, and a type on variables that start as [], null or an object you add keys to laterExample: TypeScript Output
The same code after conversion, exactly as the tool prints it:
function greet(name: any, greeting: string = 'Hello') {
return greeting + ', ' + name;
}
class Cart {
declare owner: any;
declare items: any;
constructor(owner: any) {
this.owner = owner;
this.items = [];
}
}Read the notes and tighten the types
The blue bar above the result says what was added and lists anything TypeScript will still complain about, such as functions that read arguments. Replace the any types with real ones as you get to know the code; searching for any finds every spot.
tsc --noEmit or open the file in your editor; anything left is a real question about your code, not about syntaxCopy or download the result
Copy the TypeScript or download it as converted.ts. If the code contains JSX the download is converted.tsx. Copy and Download always use the code that is in the editor at that moment.
Going the other way? TSX to JavaScript strips the types again, JSX to TSX adds props interfaces to React components, and JSON to TypeScript builds interfaces from data.
Frequently Asked Questions
What exactly does the converter add?
Only type annotations. Parameters get : any, or string, number, boolean or bigint when their default value is a literal (count = 5 becomes count: number = 5). A parameter becomes optional (name?: any) when a call in the same file leaves it out. Classes get a declare line for every field they set with this.x = ..., and plain functions that use this get a this: any parameter. Variables that start as [], as null and are reassigned, as an object you later add keys to, or as one kind of literal and later another get a matching type. That is the whole list.
Is this a full type inference engine?
No. It doesn't follow values through your program to work out that a parameter is always a string. It adds what TypeScript needs to accept the file and nothing it would have to guess, so most parameters come out as any. Treat the result as a starting point: the code compiles, and you replace each any with a real type when you know it. Callback parameters, such as the item in items.map(item => ...), are left alone because TypeScript already types them from the function they are passed to.
Does the converter change my code?
No. Every change is an insertion, so your strings, comments, regular expressions, formatting and numbers such as 8901240544102066246n, 0x1F or 14.50 come out character for character. Remove the added types again and you get the same program back, which means the converted file behaves exactly like the original at run time.
Will the TypeScript compile straight away?
For ordinary code it usually does. With noImplicitAny on (it is part of strict), one thing still shows up: a callback passed to a value typed any, like items.map(item => ...) when items is any, gets an implicit-any error until you give items or the callback a type. TypeScript can also find real problems the converter has no way to fix without changing your code: a function that reads arguments and gets called with extra values, an old-style octal number like 010, or a value whose type genuinely doesn't fit the function it is passed to. The notes above the output point out the cases the converter can spot. Run tsc on the file to see the rest.
What about JSX, React and CommonJS files?
JSX is supported: the output is TSX and downloads as converted.tsx, and React's types come from @types/react. Event handlers written inline on JSX elements keep the types React gives them. Files that use require or module.exports convert too, but TypeScript needs Node's types to understand them (npm i -D @types/node), or you can switch to import and export.
What happens if I paste broken code, TypeScript or JSON?
Broken JavaScript gets an error with the line and column and a caret under the problem, and no output, because types can only be added to code that parses. TypeScript that is already typed is recognised and left alone, and pasted JSON data is pointed to the JSON to TypeScript converter, which builds interfaces from data instead.
Is my code uploaded anywhere?
No. The conversion runs in your browser, the code is not sent to our server, and nothing is kept: reload the page and the editor starts empty.