TypeScript Formatter (tsfmt)

7.2.2 · maintenance · verified Sun Apr 19

typescript-formatter (tsfmt) is a command-line tool designed for formatting TypeScript code. It leverages the TypeScript Compiler Service API for its formatting capabilities, ensuring alignment with TypeScript's own language services. The current stable version is 7.2.2. Its release cadence appears to be moderate, with the last update (v7.2.2) released in February 2023. A key differentiator is its reliance on the official TypeScript language services, aiming for consistent formatting. It also supports reading configuration from `tsconfig.json`, `tslint.json`, `.editorconfig`, `.vscode/settings.json`, and `tsfmt.json`, allowing for highly customizable formatting rules across various project setups. It provides options for formatting files in place, outputting to stdout, or verifying file formats, making it suitable for both local development and CI/CD pipelines.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to programmatically format a TypeScript file using `typescript-formatter`'s `format` function, including configuring specific formatting options.

import { format, FormatOptions } from 'typescript-formatter';
import * as fs from 'fs';
import * as path from 'path';

async function runFormatterExample() {
  const sampleFilePath = path.join(process.cwd(), 'sample.ts');
  const initialContent = `class Sample {hello(word="world"){return "Hello, "+word;}}`;

  // Create a dummy TypeScript file
  await fs.promises.writeFile(sampleFilePath, initialContent, 'utf-8');
  console.log('Original content created:\n', initialContent);

  const options: FormatOptions = {
    // These options correspond to settings found in tsfmt.json or editorconfig
    baseIndentSize: 0,
    indentSize: 2,
    tabSize: 2,
    convertTabsToSpaces: true,
    newLineCharacter: '\n',
    insertSpaceAfterCommaDelimiter: true,
    insertSpaceAfterSemicolonInForStatements: true,
    insertSpaceBeforeAndAfterBinaryOperators: true,
    insertSpaceAfterKeywordsInControlFlowStatements: true,
    insertSpaceAfterFunctionKeywordForAnonymousFunctions: false,
    insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis: false,
    insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets: false,
    insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces: true,
    insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces: false,
    insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces: false,
    insertSpaceAfterTypeAssertion: false
  };

  try {
    // Format the content of the file programmatically
    const formattedResult = await format(sampleFilePath, initialContent, options);
    console.log('\nFormatted content:\n', formattedResult.formatted.trim());

    // Optionally, write the formatted content back to the file
    // await fs.promises.writeFile(sampleFilePath, formattedResult.formatted, 'utf-8');
    // console.log(`Formatted and replaced ${sampleFilePath}`);

  } catch (error) {
    console.error('Formatting failed:', error);
  } finally {
    // Clean up the dummy file
    await fs.promises.unlink(sampleFilePath);
    console.log(`\nCleaned up ${sampleFilePath}`);
  }
}

runFormatterExample().catch(console.error);

view raw JSON →