TypeScript to JavaScript

Strip TypeScript types from code

About TypeScript to JavaScript

TypeScript to JavaScript strips TypeScript syntax with Sucrase's parser and returns the JavaScript, in your browser. Type annotations, interfaces, type aliases, generics, access modifiers, non-null assertions, as and satisfies casts, and import type lines are removed; enums become JavaScript objects filled by a small function; constructor parameter properties become this assignments. JSX is recognized wherever it appears. Modern syntax such as optional chaining is kept, not downgraded, and the code is not type-checked. A namespace that holds only types is removed; when one holds code, which Sucrase cannot compile, the whole file is compiled by TypeScript's own compiler instead, downloaded only then.

Example

An interface, a typed function, and an as const cast.

TypeScript input

interface User { id: number; name?: string }

export function greet(u: User): string {
  return `Hi ${u.name ?? "guest"}`;
}

const ids = [1, 2] as const;

JavaScript output



export function greet(u) {
  return `Hi ${u.name ?? "guest"}`;
}

const ids = [1, 2] ;

How to convert TypeScript to JavaScript

  1. 1Paste the content of a .ts file into "TypeScript input".
  2. 2Click "Convert".
  3. 3Read "JavaScript output": it holds the code, or a line starting with Error: and the line of the problem (line:column for a syntax error).
  4. 4Click "Copy", or "Download" to save it as "script.js".

Formats and limits

Input
TypeScript source pasted as text, with or without JSX
Output
JavaScript at the same syntax level, saved as script.js
Removed
Types, interfaces, type aliases, generics, modifiers, non-null assertions, as, satisfies, import type, and unused imports
Not supported
Type checking, conversion to older JavaScript

Where your code is processed

Sucrase runs inside your browser and is downloaded only when you first click "Convert"; your TypeScript is not sent anywhere. Only for a namespace that holds code is TypeScript's own compiler (about 1.3 MB) downloaded from this site, and it also runs in your browser. Should the output show an error, the wording of that error, with quoted text, long numbers, and addresses removed, is reported to our error log with the tool name and your browser's name and version, so we can see what failed.

Frequently asked questions

Does it check my types?

No. Sucrase only removes types and never compares them, so code with type errors converts without a message. Run tsc --noEmit in your project when you need to see type errors.

Does it support TSX?

Yes. The code is first read as a .ts file, where <any>value is a cast; if that fails, it is read again as a .tsx file, so JSX works after return, after =>, in a variable or as an argument. The tags are kept as written.

Are enums kept?

Yes. Each enum becomes a variable filled by a small function, so Color.Red and reverse lookups such as Color[0] still work at run time. Const enums are converted the same way.

Are namespaces converted?

Yes. A namespace that holds only types, and a declare namespace block, are removed, as tsc removes them. A namespace that holds functions, constants, or other code becomes an object filled by a function: the whole file is then compiled by TypeScript's own compiler, with the same output as tsc, downloaded the first time it is needed (about 1.3 MB).

Tips and tricks

  • ✓Unused imports are dropped as well, but side-effect imports such as import of a stylesheet stay in the output.