TypeScript Formatter

Format and beautify TypeScript code.

Local Your input is processed locally in your browser. The first run loads the open-source Prettier library from this site; your input is not sent anywhere. Found a problem? Report an issue

What the TypeScript Formatter does

The TypeScript Formatter formats TypeScript with Prettier 3.3.3, an open-source code formatter, running in your browser. It supports TypeScript, including interfaces, generics and type annotations.

TypeScript Formatter examples

The built-in sample

Input

interface User{id:number;name?:string}
function pick<T,K extends keyof T>(o:T,k:K):T[K]{return o[k]}

Output

interface User {
  id: number;
  name?: string;
}
function pick<T, K extends keyof T>(o: T, k: K): T[K] {
  return o[k];
}

This is the input that the Load sample button fills in.

Decorators, parameter properties and an old-style cast

Input

@Component({selector:'x'})
class A{private x?:number;constructor(readonly b:string){}}
const y=<const>['a']

Output

@Component({ selector: "x" })
class A {
  private x?: number;
  constructor(readonly b: string) {}
}
const y = <const>["a"];

TypeScript-only syntax is parsed and printed, not removed: the decorator, the optional property, the readonly parameter property and the angle-bracket assertion all survive.

Each output was produced by running this tool on the input above it.

How it works

The code is parsed with Prettier's typescript parser and printed again from its syntax tree, using Prettier's default options: lines up to 80 characters and two-space indentation. The result therefore does not depend on how the input was laid out. If the code cannot be parsed, the parser's message with its line and column is shown instead of output.

Details and limits

  • Only the syntax is read. Types are not checked, so code with type errors formats without complaint.
  • Object types and interfaces get one member per line once they no longer fit on one line.
  • Plain JavaScript and JSX are valid input as well, since TypeScript is a superset of JavaScript.

Questions

Is my data uploaded when I use TypeScript Formatter?

No. The tool runs in your browser, so what you paste or open is processed on your own device and is not sent to a server.

What does TypeScript Formatter do?

The TypeScript Formatter formats TypeScript with Prettier 3.3.3, an open-source code formatter, running in your browser. It supports TypeScript, including interfaces, generics and type annotations.

How does TypeScript Formatter work?

The code is parsed with Prettier's typescript parser and printed again from its syntax tree, using Prettier's default options: lines up to 80 characters and two-space indentation. The result therefore does not depend on how the input was laid out.

Are there any limits I should know about?

Only the syntax is read. Types are not checked, so code with type errors formats without complaint. Object types and interfaces get one member per line once they no longer fit on one line.

Is there a size limit?

There is no fixed size limit. Everything happens on your device, so very large inputs depend on its memory and can take longer on a phone than on a computer.

Does it work on phones, and is it free?

Yes. It works in any current browser on Android phones, iPhones, tablets and computers. It is free and needs no account or sign-up.