TypeScript Formatter

Format TypeScript code.

FreeNo accountOn your device

Runs in your browser. How to verify

Loading tool…

About this tool

Paste TypeScript and format it with Prettier's TypeScript parser. Interfaces, generics, enums, decorators and type annotations are understood, not just indented. Every Prettier option that applies to TypeScript is available, including tab width, print width, quotes, quote props, trailing commas, arrow parentheses, object wrapping and line endings. A syntax error is reported with its line and column, and your code never leaves the browser.

How to use

1
Step 1
Paste your TypeScript into the Input box.
2
Step 2
Set tab width, print width, semicolons and quotes, or open All Prettier options for the rest.
3
Step 3
Press Format, then copy or download the result and check the bytes before and after.
4
Step 4
Press Copy as.prettierrc (JSON) for your non-default options, or Reset to Prettier defaults to start over.

How it works

The source is parsed with Prettier's typescript parser, which builds a full syntax tree including types, and printed again using your options. That is why interface members each land on their own line, generics are spaced correctly and long signatures wrap at the print width. The parser also accepts TSX, and you can switch to babel-ts if you prefer it. Only the typescript plugin (or Babel, for babel-ts) is downloaded, and your option choices are remembered in this browser. If Prettier cannot load, a basic indentation formatter is used and a note appears.

Worked example

Interface and generic function

Input
interface User{id:number;name:string} function get<T>(x:T):T{return x}
Output
interface User { id: number; name: string; } function get<T>(x: T): T { return x; }
Walkthrough
Each interface member moves to its own line, the colon spacing is normalised and the generic function body is indented.

Limitations

  • Code with a syntax error is not formatted; the message points to the line and column.
  • Type-checking is not performed: valid syntax with type errors still formats.
  • Only options that apply to TypeScript are shown; overrides, plugins and.prettierignore are not.
  • Very large files may take a moment because formatting runs in your browser.

Questions

Does it understand TypeScript types?
Yes. It uses Prettier's TypeScript parser, so interfaces, generics, unions and enums are formatted as a project using Prettier would.
Can it format TSX?
Yes, the TypeScript parser accepts JSX syntax as well.
Will it check my types?
No. It checks syntax only. Use tsc or your editor to find type errors.
How do I remove semicolons?
Untick Semicolons and press Format.
Can I export my settings?
Yes. Copy as.prettierrc (JSON) outputs only options that differ from Prettier's defaults. Reset to Prettier defaults restores everything, and your choices are otherwise remembered in this browser.
Which options are available?
Tab width, tabs, print width, semicolons, quotes, quote props, trailing commas, bracket spacing, arrow parentheses, object wrap, end of line, embedded language formatting and the experimental ternaries and operator-position styles. The JSX-only ones apply to TSX.
Is my code uploaded?
No. Everything runs in your browser.

Features

  • Real Prettier engine
  • Interfaces and generics
  • Every TypeScript Prettier option
  • typescript or babel-ts parser
  • Copy as.prettierrc with only non-default options
  • Reset to Prettier defaults
  • Bytes before and after
  • Line and column for errors
  • Runs in your browser

Related resources

Guides and videos from around the web—opens on YouTube or the publisher’s site.

  • How base64 encoding works

    Andrew Schmelyun

    Mental model for Base64 encode/decode tools.

    Watch on YouTube
  • TypeScript in 100 Seconds

    Fireship

    Formatted code is easier to skim while learning typed JS.

    Watch on YouTube