Compiled in your browser, line for line
The code is compiled with Sucrase, which removes type annotations and turns TypeScript-only features (enums, parameter properties, namespaces) into JavaScript, keeping every statement on its original line. Errors and stack traces therefore point at the line you wrote.
It then runs in a sandboxed Web Worker, exactly as in the JavaScript Playground: top-level await works, each run starts fresh, and a time limit stops runaway code.
How to use the TypeScript Playground
- Write or paste TypeScript in the editor, or load an example.
- Press Run, or Ctrl+Enter (⌘+Enter on a Mac).
- Read the output in the console: click an object or array to expand it.
- Fix any syntax error shown with its line and column, and run again.
- Press Copy share link to share the code: it goes in the link itself.
Examples
A generic groupBy
interface User {
id: number;
name: string;
role: "admin" | "editor";
}
function groupBy<T, K extends keyof T>(items: T[], key: K): Map<T[K], T[]> {
const groups = new Map<T[K], T[]>();
for (const item of items) {
const list = groups.get(item[key]) ?? [];
list.push(item);
groups.set(item[key], list);
}
return groups;
}
const users: User[] = [
{ id: 1, name: "Ada", role: "admin" },
{ id: 2, name: "Linus", role: "editor" },
{ id: 3, name: "Grace", role: "admin" },
];
console.log(groupBy(users, "role"));Map(2) {'admin' => [{id: 1, name: 'Ada', role: 'admin'}, {id: 3, name: 'Grace', role: 'admin'}], 'editor' => [{id: 2, name: 'Linus', role: 'editor'}]}In the console the Map's arrays are collapsed; click to expand them.
Narrowing a discriminated union
type Shape =
| { kind: "circle"; radius: number }
| { kind: "rect"; width: number; height: number };
function area(shape: Shape): number {
switch (shape.kind) {
case "circle":
return Math.PI * shape.radius ** 2;
case "rect":
return shape.width * shape.height;
}
}
const shapes: Shape[] = [
{ kind: "circle", radius: 1 },
{ kind: "rect", width: 3, height: 4 },
];
for (const shape of shapes) {
console.log(shape.kind, area(shape).toFixed(2));
}circle 3.14
rect 12.00Types are removed, not checked
This playground runs your code; it does not type-check it. const n: number = "five" runs and logs five. That keeps it instant, but use your editor or tsc --noEmit when you need the compiler's errors. Syntax errors are still reported, with their line and column.
Safe to run code from anywhere
Code runs in a frame with an opaque origin and a strict Content Security Policy, so it can't read this page, your cookies or saved tools, can't open windows or navigate, and can't make network requests. Stopping a run discards the sandbox.
Options
- Time limit
- How long a run may last: 5, 10 or 30 seconds. A run that is still busy, or still has timers pending, is stopped when it is reached.
- Examples
- Load ready-made code: generics, discriminated unions, classes and enums, typed async code and
satisfies.
Limitations
- Types are not checked: type errors don't stop the code from running.
- Decorators and
import/exportare not supported: the code is a single file that runs on its own. - No network access and no DOM: the code runs in a sandboxed Web Worker.
evalandnew Functionare blocked. - Output stops after 1,000 lines or 1 MB per run.
Frequently asked questions
How do I share code?
Press Copy share link. The code is compressed into the link itself, after the #, which browsers never send to a server, so nothing is uploaded or stored. Opening the link loads the code into the editor, replacing the code saved in that browser. Very long code makes long links, which some chat apps cut off.
Does it show type errors?
No. It strips the types and runs the code, so it starts instantly. Use your editor or tsc --noEmit for type checking. Syntax errors are reported with their line and column.
Which TypeScript features work?
Everything that compiles to JavaScript by removing types: interfaces, type aliases, generics, unions, as, satisfies, non-null assertions, enums, parameter properties and namespaces.
Is my code uploaded?
No. It is compiled and run in your browser, and saved only in this browser's local storage so it's still there next time. Nothing you type is sent anywhere.
Can I use top-level await?
Yes. await works at the top level, as in a module; see the typed async example.