JS to TypeScript

Adds basic TypeScript annotations to JavaScript — a starting point for a migration rather than a finished conversion. What it cannot do is as important as what it can, because a half-typed file that claims to be typed is worse than an untyped one.

Live demo · real result from this tool
When the type is unknown

TypeScript (first draft)

/** * @param {number} price * @param {number} [rate] * @returns {number} */function withTax(price: number, rate: number = 0.18): number {  return price * (1 + rate);} const tags: unknown[] = [];const label = (item: unknown, prefix: string = '#') => prefix + item.name; 
Annotations added6
From JSDoc
3

A starting point: rename the file to .ts, run tsc with strict on, and fix what it reports. Types are erased at runtime, so validate external data (e.g. with Zod) as well.

Processed on your device — nothing is uploaded.

How to use JS to TypeScript

  1. Paste your JavaScript.
  2. Read the annotated output as a first draft.
  3. Rename the file, run the compiler, and fix what it reports — that is the real conversion.
  4. Inferred types are a starting point; the compiler, not a converter, does the real work.
  5. Prefer unknown to any when you cannot determine a type — it forces narrowing rather than disabling checks.
  6. Types are erased at compile time and provide no runtime validation.

Migrate incrementally, which TypeScript is designed for

You do not convert a codebase in one pass. Rename a file to .ts, let the compiler tell you what it cannot infer, fix those, and move on — with allowJs on so typed and untyped files coexist. Start at the leaves, with utilities and types that nothing depends on, and work toward the entry points; converting a module before its dependencies means writing types twice. The compiler is the migration tool, and a converter only saves you the mechanical first pass.

any is a hole in the type system, and it spreads

Annotating something as any turns off checking for it and everything derived from it, which makes the error go away and removes the value you converted for. unknown is the honest alternative: it accepts anything, like any, but forces you to narrow it before use, so the check happens where the data enters rather than nowhere. Turn on noImplicitAny early, before the codebase fills with silent anys that are painful to find later.

The compiler options that decide how much this is worth

strict is the one that matters, and it is worth turning on at the start of a migration rather than after, because retrofitting strictness to a large converted codebase is miserable. Its most valuable component is strictNullChecks, which separates a value from a value-or-null and catches the single most common class of runtime error in JavaScript. Without it, TypeScript will happily let you call a method on something that might be undefined.

Types vanish at runtime

TypeScript compiles to JavaScript by deleting the annotations. There is no runtime checking whatsoever, so a server that returns something other than what your interface promises produces an application that compiled cleanly and crashes anyway. Anything crossing a boundary — an API response, a form submission, parsed JSON, a message from another window — needs a runtime validator. Zod and Valibot let you define the schema once and derive the type from it, which keeps them from drifting apart.

Frequently asked questions

What TypeScript features are added?
The tool adds basic type annotations to function parameters and return types, converts var to let/const, and adds interface suggestions.
Is the output production-ready TypeScript?
The output is a starting point. You'll need to refine types, add generics, and handle edge cases for production TypeScript code.
Do I need to know TypeScript to use this?
Basic TypeScript knowledge helps. The tool generates simple type annotations that you can then customize and improve.
Can I convert a whole project automatically?
Not meaningfully. Tools produce a mechanical first pass, and the value of TypeScript comes from the types the compiler cannot infer — function contracts, union types, nullability. Convert file by file with allowJs enabled, starting from the dependency leaves.
What is the difference between any and unknown?
any disables type checking for that value and everything derived from it. unknown also accepts anything, but will not let you use it until you narrow it to a specific type. unknown is almost always the right choice when you genuinely do not know.
Does TypeScript check types at runtime?
No. Annotations are erased during compilation, leaving plain JavaScript. A type is a promise about your own code, not a guarantee about data arriving from outside it — use a runtime validator at every boundary.

Written by the ToolsRift team · Last updated

Pages are drafted with AI assistance and checked by automated tests. How we write and check pages.

Was this tool useful?

Report a problem

Start typing to search every tool.

↑ ↓ to moveEnter to openEsc to close