TypeScript Formatter
Format TypeScript code.
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
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?
Can it format TSX?
Will it check my types?
How do I remove semicolons?
Can I export my settings?
Which options are available?
Is my code uploaded?
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 YouTubeTypeScript in 100 Seconds
Fireship
Formatted code is easier to skim while learning typed JS.
Watch on YouTube
- PostgreSQL documentation
PostgreSQL
SQL dialect reference when a formatter reshapes a query for review.
- JSON.stringify()
MDN
Pretty-print spacing options that JSON formatters emulate.