TypeScript Formatter
Indent and format TypeScript and JavaScript with preserved line breaks.
- Runs locally in your browser
- No upload required
Try it
Supported formats
Privacy
Runs entirely in your browser. Your input is never uploaded, stored, or sent to a third party.
Re-indented 3 line(s) of TypeScript at 2-space depth.
How it works
- Tokenised the source so strings, template literals, comments and regular expressions are recognised.
- Tracked brace depth and placed each statement on its own line.
- Preserved the author's line breaks so automatic semicolon insertion cannot change.
- Left type annotations, generics and template-literal contents untouched.
- The formatter changes whitespace only; identifiers, types and literals are preserved.
- Existing line breaks are kept because moving a token across a newline can change automatic semicolon insertion.
- Output is 3 line(s); formatting it again changes nothing.
Methodology
- Tokenise with a quote/regex/comment-aware scanner so literal content cannot be mistaken for punctuation.
- Track brace depth outside parentheses and brackets and emit each statement on its own line.
- Preserve the original newline positions so automatic semicolon insertion semantics are unchanged.
Usage
- Paste or type a TypeScript or JavaScript file.
- Choose a 2- or 4-space indent.
- Run the formatter and copy the indented result.
Examples
These are examples, not current values: they use fixed inputs, while the interactive tool computes live results in your browser.
Function with a type annotation
{
"text": "function add(a: number, b: number): number {\nreturn a + b\n}",
"indentSize": 2
}Interface indented
{
"text": "interface User {\nname: string\nage: number\n}",
"indentSize": 2
}Template literal preserved
{
"text": "const s = `a\n b\n ${x}`",
"indentSize": 4
}Limitations
Automatic semicolon insertion
JavaScript and TypeScript insert a semicolon at a line break in specific positions. A formatter that moves statements across newlines can silently change a program's meaning, which is why this tool preserves existing line breaks.
Combined with brace-based indentation, the result is readable without risking the statement semantics.
Frequently asked questions
- Does it format both TypeScript and JavaScript?
- Yes. TypeScript is a superset of JavaScript, and the same tokenizer handles both, including type annotations and generics.
- Why are my line breaks kept?
- Moving a token across a newline can change automatic semicolon insertion, so the formatter never removes or adds a break that could change behaviour.
- Is my code uploaded?
- No. All processing happens in your browser and nothing is sent anywhere.
Sources
- ECMA-262 Language Specification — TC39
- TypeScript Documentation — Microsoft
Send feedback
Tell us if this tool helped, report a problem, or suggest a tool. Only the message you type is sent — never the input you used in the tool.
Was this tool useful?
Prefer to write to us directly? See the contact page.