TypeScript Formatter Online — Format TS Code

Beautify TypeScript with 2-space Prettier-style indentation. Format interfaces, type aliases, generics, decorators, classes, and TSX — 100% in your browser.

What is a TypeScript Formatter?

A TypeScript formatter rewrites TS source with consistent indentation, brace placement, and line breaks — turning a one-line interface, a tangled generic constraint, or a dense decorator stack into something readable. It targets TypeScript-specific syntax that a generic JS beautifier misses: interface, type, generic parameters with extends bounds, decorators, enum bodies, and TSX.

The OpenFormatter TypeScript formatter is a pretty printer — it preserves your token order and only adjusts whitespace and brace layout, so behaviour is identical and the diff is purely additive. It runs entirely in your browser; nothing is uploaded, no account is needed, no rate limits apply.

How to format TypeScript online — 4 steps

  1. Paste your TypeScript. Drop TS or TSX into the Input panel — interfaces, type aliases, generics, classes, decorators, async functions. Click Load Sample to try a generic-heavy demo.
  2. Click Format. The formatter rewrites brace placement and indentation client-side and emits one statement per line.
  3. Review the output. Confirm nested generics are readable, interfaces have one member per line, and decorators sit on their own line above the declaration.
  4. Copy the result. Click Copy to paste into your editor, a PR description, a GitHub issue, or a snippet for your team chat.

Side-by-side: dense input vs formatted output

Compact TypeScript

interface User<T extends string>{id:T;name:string;email?:string;}
type Result<T,E=Error>={ok:true;value:T}|{ok:false;error:E};
function fetchUser<T extends string>(id:T):Promise<Result<User<T>>>{return fetch(`/api/user/${id}`).then(r=>r.json()).then(value=>({ok:true,value})).catch(error=>({ok:false,error}));}
class Cache<K,V>{private store=new Map<K,V>();get(k:K):V|undefined{return this.store.get(k);}set(k:K,v:V):void{this.store.set(k,v);}}

Formatted TypeScript

interface User<T extends string> {
  id: T;
  name: string;
  email?: string;
}
type Result<T, E = Error> = { ok: true; value: T } | { ok: false; error: E };
function fetchUser<T extends string>(id: T): Promise<Result<User<T>>> {
  return fetch(`/api/user/${id}`).then(r => r.json()).then(value => ({ ok: true, value })).catch(error => ({ ok: false, error }));
}
class Cache<K, V> {
  private store = new Map<K, V>();
  get(k: K): V | undefined {
    return this.store.get(k);
  }
  set(k: K, v: V): void {
    this.store.set(k, v);
  }
}

TypeScript-Aware

Handles interface, type, enum, generic constraints, decorators, and class members — the syntax that a generic JS beautifier mangles or skips.

Compilation Unchanged

Whitespace outside string and template-literal contexts does not affect tsc emit. Formatted and unformatted source compile to identical JavaScript.

Browser-Only

TypeScript runs in your browser. Code with API keys, proprietary types, or unreleased features never leaves the device.

Common use cases

  • check_circleFormat TypeScript before pasting into a Prettier-enforced project so the diff stays small
  • check_circleReformat dense generic-heavy TypeScript copied from Stack Overflow or a GitHub issue
  • check_circleClean up auto-generated TS from openapi-typescript, graphql-codegen, or zod schemas
  • check_circleIndent a decorator-heavy NestJS controller for a code-review screenshot
  • check_circleFormat TSX from a React component before pasting into a documentation site
  • check_circleReformat TypeScript types extracted from .d.ts ambient declaration files
  • check_circleClean up a one-line interface chain pasted from a TypeScript Playground share link
  • check_circleBeautify a Zod schema or io-ts codec where the chained .extend().refine() got compacted

TypeScript formatter vs Prettier vs ESLint

A pretty printer is the lightweight option — it preserves token order and only adjusts whitespace, so the diff is small and the output predictable. Prettier goes further: it re-prints the AST, normalises quote style, enforces line length, applies trailing commas, and splits long generic constraints. ESLint with @typescript-eslint is a different tool entirely — it checks style and correctness rules (unused vars, prefer-const, no-explicit-any) and can autofix some, but is not primarily a formatter. Use this tool for a quick one-off reformat; use Prettier for project-wide enforcement; use ESLint for catching bugs and enforcing patterns.

More than TypeScript formatting

Pretty print JS, beautify with full options, or convert JSON to TypeScript types — all browser-side.

Frequently Asked Questions

What is the difference between an interface and a type alias?

Both describe the shape of an object. Interfaces support declaration merging — multiple interface User { … } blocks in scope combine into one — and they always have a stable, named identity in error messages. Type aliases support unions (type X = A | B), intersections, mapped types, and conditional types that interfaces cannot express. Use interface for object/class contracts that may need extension by consumers; use type for unions, primitives, tuples, and computed types. The formatter treats both identically — both get a brace, indent, and one-member-per-line layout.

What are the conventions for spacing inside generics?

The Prettier / TSLint convention this formatter follows: no space inside the angle brackets (Array<string>, not Array< string >), one space after each comma in a multi-parameter list (Map<K, V>), and one space around the equals sign in a default (T extends string = "id"). Constraints use a space around extends (T extends string). The formatter normalises commas and equals; angle brackets are left as written.

Does the formatter preserve comments?

Yes. Single-line // comments, multi-line /* … */ blocks, and JSDoc /** … */ docstrings are preserved in place. They are emitted at the indent of the line they sit on. The formatter never strips, reorders, or splits comments — important for codebases that rely on JSDoc for type narrowing or for ESLint disable directives at the end of a line.

Does formatting change the compiled JavaScript?

No. TypeScript outside string and template-literal contexts is whitespace-insensitive. The compiler tsc emits identical JavaScript from formatted and unformatted source — the same tokens, the same AST, the same emit. The only thing that changes is human readability and the line-by-line diff in version control.

Does this handle TSX (React + TypeScript) files?

Yes. The brace-aware formatter does not interpret JSX as text — it follows the surrounding curly braces, so a return ( <Component prop={value} /> ) gets indented as a function body and the JSX is left intact. For deeper JSX-specific reformatting (attribute wrapping, self-closing normalisation), Prettier remains the better choice.

How is this different from running Prettier?

This is a lightweight pretty printer — it preserves your token order and only adjusts indent and brace placement. Prettier is opinionated: it re-prints the entire AST, normalises quote style, enforces line length, applies trailing commas, and splits long expressions across multiple lines. For a one-off readability pass on a snippet, this tool is faster. For project-wide enforcement, configure Prettier with a .prettierrc and a pre-commit hook so every file is normalised before it lands in the repo.

Will it handle decorators and the experimental decorator metadata?

Yes. Decorators on classes (@Component({…})), methods (@Get("/users")), and parameters (constructor(@Inject(TOKEN) svc: Service)) are formatted on their own line above the decorated declaration when they sit at indent level zero of the body. The new ECMAScript decorators proposal and the legacy experimentalDecorators flag use the same surface syntax, so both are handled.

Is the TypeScript I paste sent to your servers?

No. Formatting runs entirely in JavaScript on your machine. Code containing API keys, proprietary algorithms, or unreleased TypeScript features never leaves your browser. Open DevTools → Network and click Format to confirm no requests are made.

TypeScript Formatter Online — Format TS Code | OpenFormatter