Ttooleras
📘

TypeScript Formatter

Formatters & Beautifiers

Format TypeScript and TSX with type annotations, interfaces, generics preserved. Free, private — all processing in your browser.

Advertisement

The TypeScript Formatter beautifies TypeScript (.ts) and TSX (.tsx) code while correctly handling type-specific syntax: type annotations (: string), interfaces, type aliases, generics (<T>), union and intersection types (A | B, A & B), enums, decorators (@Component), namespaces, const assertions, and satisfies operator. Output follows Prettier conventions which have become the de facto TypeScript style (used by most modern TS projects including Next.js, NestJS, Vue 3, Angular). Complements our JavaScript Formatter — TypeScript is a superset of JavaScript, so most rules overlap.

TypeScript has been adopted by the majority of new JavaScript projects since ~2020 — adding static typing to catch bugs at compile time. The additional type syntax makes formatting slightly more complex than plain JS, but Prettier handles it well. This formatter respects both JavaScript rules and TypeScript-specific patterns — complex generic constraints on multiple lines, long union types broken up, decorator placement, class field types.

TypeScript Formatter — key features

Full TypeScript support

Types, interfaces, generics, enums, decorators, namespaces, modern TS features.

TSX for React

TypeScript + JSX format correctly — components with typed props.

Generic constraints

Complex <T extends ...> constraints formatted for readability.

Long union types

A | B | C | D auto-wraps onto multiple lines for readable code.

Decorator handling

Class and property decorators correctly positioned and formatted.

Preserves semantics

Types unchanged. Behavior identical. Only whitespace reformatted.

Modern TS features

Satisfies operator, template literal types, const assertions, mapped types.

100% client-side

Your TypeScript (proprietary types, business logic) stays in browser.

How to use the TypeScript Formatter

  1. 1

    Paste TypeScript or TSX

    From editor, file, or copy-paste. Modern TS features supported.

  2. 2

    Select language mode

    TypeScript or TSX (for React components).

  3. 3

    Configure style

    2 vs 4 spaces, quote style, semicolons. Defaults match Prettier.

  4. 4

    Click Format

    Cleanly formatted TypeScript preserving all type information.

  5. 5

    Copy or download

    Save as .ts or .tsx file. Ready for your codebase.

Common use cases for the TypeScript Formatter

Modern frameworks

  • Next.js projects: Most Next.js projects use TypeScript. Format before committing.
  • React TypeScript: TSX components with typed props and hooks.
  • NestJS backends: NestJS is TypeScript-first. Decorators, injectable classes — all formatted correctly.
  • Angular: Angular TypeScript with decorators and DI.

Library development

  • Type definitions: Publishing a TS library — types need to be clean for consumers.
  • Declaration files: .d.ts files for vanilla JS libraries.
  • Exported types: Public-facing types should be well-formatted for developer experience.

Debugging and learning

  • Understand TypeScript output: See how complex types look formatted.
  • Convert JS to TS patterns: Format while migrating a JS codebase to TypeScript.
  • Code review prep: Format before PR so reviewers focus on types and logic.

Team standards

  • Consistent team style: Everyone uses Prettier format — eliminates style arguments.
  • Onboarding new developers: Show clean formatted examples to new team members.
  • Migrating legacy code: Format old TypeScript files to modern conventions.

TypeScript Formatter — examples

Interface formatting

Properties one per line.

Input
interface User{id:number;name:string;email?:string;readonly createdAt:Date;}
Output
interface User {
  id: number;
  name: string;
  email?: string;
  readonly createdAt: Date;
}

Generic function

Type parameters and constraints.

Input
function mapArray<T,U>(arr:T[],fn:(item:T)=>U):U[]{return arr.map(fn);}
Output
function mapArray<T, U>(arr: T[], fn: (item: T) => U): U[] {
  return arr.map(fn);
}

Union types

Long unions on separate lines.

Input
type Result={status:"success";data:User}|{status:"error";message:string}|{status:"pending"};
Output
type Result =
  | { status: "success"; data: User }
  | { status: "error"; message: string }
  | { status: "pending" };

Class with decorators

Angular-style component.

Input
@Component({selector:"app-home",template:`<h1>Home</h1>`})class HomeComponent{@Input()title:string;@Output()changed=new EventEmitter<string>();}
Output
@Component({
  selector: "app-home",
  template: `<h1>Home</h1>`,
})
class HomeComponent {
  @Input() title: string;
  @Output() changed = new EventEmitter<string>();
}

TSX component

React with props type.

Input
const Button:React.FC<{label:string;onClick:()=>void}>=({label,onClick})=><button onClick={onClick}>{label}</button>;
Output
const Button: React.FC<{ label: string; onClick: () => void }> = ({
  label,
  onClick,
}) => <button onClick={onClick}>{label}</button>;

Enum

Members one per line.

Input
enum Status{Active="ACTIVE",Inactive="INACTIVE",Pending="PENDING"}
Output
enum Status {
  Active = "ACTIVE",
  Inactive = "INACTIVE",
  Pending = "PENDING",
}

Technical details

TypeScript adds type annotations and several type-system features on top of JavaScript.

Type annotations:

``typescript
let name: string = "Alice";
function add(a: number, b: number): number { return a + b; }
``

Formatter preserves annotations, spacing consistent: name: string (space after colon).

Interfaces:

``typescript
interface User {
id: number;
name: string;
email?: string; // optional
readonly created: Date;
}
``

Formatter formats members like object literals — one per line, consistent spacing.

Type aliases:

``typescript
type UserId = number;
type Coordinates = { x: number; y: number };
type Status = "active" | "inactive" | "pending";
``

Union and intersection types:

Long union types break onto multiple lines:

``typescript
type Result =
| { status: "success"; data: User }
| { status: "error"; message: string }
| { status: "pending" };
``

Generics:

```typescript
function identity<T>(value: T): T { return value; }

interface Repository<T, K extends keyof T = "id"> {
findById(id: K): T | null;
}
```

Constraints (extends) formatted consistently.

Enums:

``typescript
enum Status {
Active = "ACTIVE",
Inactive = "INACTIVE",
Pending = "PENDING",
}
``

Or const enums:

``typescript
const enum Direction {
Up,
Down,
Left,
Right,
}
``

Decorators:

``typescript
@Component({
selector: "app-home",
template:
<h1>Home</h1>,
})
class HomeComponent {
@Input() title: string;
@Output() changed = new EventEmitter<string>();
}
``

TSX (React TypeScript):

Same as JSX with type annotations on props:

```typescript
interface ButtonProps {
label: string;
onClick: () => void;
variant?: "primary" | "secondary";
}

const Button: React.FC<ButtonProps> = ({ label, onClick, variant = "primary" }) => (
<button className={btn btn-${variant}} onClick={onClick}>
{label}
</button>
);
```

Modern TS features (2022+):

- Satisfies operator: const config = { ... } satisfies Config;
- Const type parameters: function create<const T>(value: T): T.
- Using declarations: using resource = getResource(); (explicit resource management).
- Template literal types: type Color = \#\${string}\;.
- Conditional types: type IsArray<T> = T extends unknown[] ? true : false;.
- Mapped types: type Partial<T> = { [K in keyof T]?: T[K] };.

All handled correctly by Prettier-based formatting.

Prettier TypeScript defaults (same as JS):

- 2-space indent
- Double quotes
- Semicolons required
- Trailing commas (all, ES2017+)
- 80-character line width

Prettier disagrees with some ESLint rules:

Conflicts with ESLint no-extra-parens, etc. Use eslint-config-prettier to disable formatting rules in ESLint. Prettier formats, ESLint checks semantics.

Common problems and solutions

Generic constraints not wrapping

Very long constraints may not wrap as expected. Formatter tries to preserve readability but complex generics are hard. Refactor into named types if too complex.

JSX and types mix

Type annotations in JSX can format surprisingly. Extract complex types to separate declarations for cleaner JSX.

Decorator alignment

Multiple decorators on one declaration may stack oddly. Use parameter properties and destructuring to reduce decorator clutter.

Strings with special chars

Template literals and strings with backticks, dollar signs may confuse formatter. Escape properly or use string types.

eslint-prettier conflicts

Some ESLint rules conflict with Prettier. Use eslint-config-prettier to disable conflicting rules. Prettier for style, ESLint for semantics.

Modern TS features unfamiliar

Satisfies, const type params, using — relatively new. Older Prettier versions may not support. Update to latest Prettier.

Auto-import changes

Formatter does not add imports (that is ts-language-service job). Combine with ts-language-server for import management.

Type narrowing complexity

Complex conditional types (Extract, Exclude, Parameters, ReturnType) format on multiple lines. Helps readability but can be surprising.

TypeScript Formatter — comparisons and alternatives

TypeScript Formatter vs JavaScript Formatter: Same Prettier base. TS formatter adds type syntax handling. For .ts files, use TS. For .js, either works (TS handles both).

Prettier vs tsfmt (legacy): tsfmt is an older TypeScript formatter. Prettier is the modern standard used by virtually all modern TS projects.

Formatter vs TypeScript compiler: tsc compiles TS to JS. This formatter formats TS as TS. Different purposes.

TypeScript vs JavaScript: TS is superset of JS. All JS is valid TS (with allowJs: true). This formatter handles both.

This tool vs editor integration: VS Code has Prettier built-in. Format on save is ideal for daily work. This tool for ad-hoc formatting without VS Code open.

Formatter vs Linter: Formatter fixes style. Linter (ESLint with @typescript-eslint) fixes semantic issues. Both needed.

Frequently asked questions about the TypeScript Formatter

Does it format TSX (React TypeScript)?

Yes. TSX files (TypeScript + JSX) formatted with combined rules. Type annotations on props, hooks, and component signatures handled correctly.

Does it preserve my type annotations?

Yes, exactly. Types, interfaces, generics, union/intersection types — all preserved. Formatter only changes whitespace and line breaks, not type logic.

Is my TypeScript code safe here?

Yes. Formatting happens in your browser. Code never uploads. Safe for proprietary type definitions, internal APIs, business logic.

What version of TypeScript is supported?

Modern TypeScript 5.x features including: satisfies operator, const type parameters, using declarations, template literal types, conditional types, mapped types, decorators. Older TS also works.

Same rules as Prettier?

Compatible. Uses Prettier conventions: 2-space indent, double quotes, semicolons, trailing commas. If your project has a .prettierrc, configure the tool to match.

Does it support decorators?

Yes. Class decorators, property decorators, parameter decorators — all formatted correctly. Angular, NestJS, TypeORM patterns supported.

Can I disable variable mangling?

Formatters do not mangle — that is minifier territory. Formatter only changes whitespace. Variable names preserved exactly.

How does it handle generics?

Short generics on one line: <T>. Complex generics with constraints may span multiple lines: <T extends SomeType, U = DefaultType>. Consistent spacing.

What about enum formatting?

Members one per line with values aligned. Both regular and const enums supported. Trailing commas added.

Does it check my types for errors?

No. Formatter only fixes style. For type checking, use tsc or your IDE. Formatter runs before type checker in your workflow.

Additional resources

Advertisement

Learn more

Explore more tools

200+ free tools that run in your browser.

Browse all tools →