{"id":12760,"library":"typescript-formatter","title":"TypeScript Formatter (tsfmt)","description":"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.","status":"maintenance","version":"7.2.2","language":"javascript","source_language":"en","source_url":"ssh://git@github.com/vvakame/typescript-formatter","tags":["javascript","TypeScript"],"install":[{"cmd":"npm install typescript-formatter","lang":"bash","label":"npm"},{"cmd":"yarn add typescript-formatter","lang":"bash","label":"yarn"},{"cmd":"pnpm add typescript-formatter","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Required for parsing and formatting TypeScript code; different versions may lead to different formatting results or errors due to changes in the TypeScript Compiler Service API.","package":"typescript","optional":false}],"imports":[{"note":"While primarily a CLI tool, `format` provides programmatic access to format a single TypeScript code string. CommonJS `require` might work in older Node.js contexts, but ESM `import` is preferred for modern TypeScript projects.","wrong":"const { format } = require('typescript-formatter');","symbol":"format","correct":"import { format } from 'typescript-formatter';"},{"note":"Used for programmatic processing and formatting of multiple files, mirroring the CLI behavior. This function typically takes a list of file paths and options.","wrong":"const processFiles = require('typescript-formatter').processFiles;","symbol":"processFiles","correct":"import { processFiles } from 'typescript-formatter';"},{"note":"Type import for configuring the detailed formatting behavior when using the programmatic `format` or `processFiles` functions. This type defines properties such as indent size, tab style, and space insertion rules, reflecting the options available via `tsfmt.json`.","symbol":"FormatOptions","correct":"import { FormatOptions } from 'typescript-formatter';"}],"quickstart":{"code":"import { format, FormatOptions } from 'typescript-formatter';\nimport * as fs from 'fs';\nimport * as path from 'path';\n\nasync function runFormatterExample() {\n  const sampleFilePath = path.join(process.cwd(), 'sample.ts');\n  const initialContent = `class Sample {hello(word=\"world\"){return \"Hello, \"+word;}}`;\n\n  // Create a dummy TypeScript file\n  await fs.promises.writeFile(sampleFilePath, initialContent, 'utf-8');\n  console.log('Original content created:\\n', initialContent);\n\n  const options: FormatOptions = {\n    // These options correspond to settings found in tsfmt.json or editorconfig\n    baseIndentSize: 0,\n    indentSize: 2,\n    tabSize: 2,\n    convertTabsToSpaces: true,\n    newLineCharacter: '\\n',\n    insertSpaceAfterCommaDelimiter: true,\n    insertSpaceAfterSemicolonInForStatements: true,\n    insertSpaceBeforeAndAfterBinaryOperators: true,\n    insertSpaceAfterKeywordsInControlFlowStatements: true,\n    insertSpaceAfterFunctionKeywordForAnonymousFunctions: false,\n    insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis: false,\n    insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets: false,\n    insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces: true,\n    insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces: false,\n    insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces: false,\n    insertSpaceAfterTypeAssertion: false\n  };\n\n  try {\n    // Format the content of the file programmatically\n    const formattedResult = await format(sampleFilePath, initialContent, options);\n    console.log('\\nFormatted content:\\n', formattedResult.formatted.trim());\n\n    // Optionally, write the formatted content back to the file\n    // await fs.promises.writeFile(sampleFilePath, formattedResult.formatted, 'utf-8');\n    // console.log(`Formatted and replaced ${sampleFilePath}`);\n\n  } catch (error) {\n    console.error('Formatting failed:', error);\n  } finally {\n    // Clean up the dummy file\n    await fs.promises.unlink(sampleFilePath);\n    console.log(`\\nCleaned up ${sampleFilePath}`);\n  }\n}\n\nrunFormatterExample().catch(console.error);","lang":"typescript","description":"This quickstart demonstrates how to programmatically format a TypeScript file using `typescript-formatter`'s `format` function, including configuring specific formatting options."},"warnings":[{"fix":"Always ensure your project's `typescript` peer dependency aligns with the version range specified by `typescript-formatter`. Upgrade `typescript-formatter` when updating your `typescript` version to maintain compatibility.","message":"Major TypeScript version upgrades can cause `typescript-formatter` to behave unexpectedly or fail, as it relies on the internal TypeScript Compiler Service API which can have breaking changes.","severity":"breaking","affected_versions":">=1.0.0"},{"fix":"Use the `--verbose` flag to debug which configuration files are being loaded and applied. Explicitly disable unwanted config sources using flags like `--no-tslint` or `--no-editorconfig` to isolate formatting rules.","message":"When multiple configuration files (e.g., `tsfmt.json`, `tsconfig.json`, `tslint.json`, `.editorconfig`, `.vscode/settings.json`) exist, their settings can override each other, leading to unexpected formatting results.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Consider evaluating alternative, more actively maintained formatters like Prettier if rapid updates or extensive new features are critical for your workflow.","message":"The project appears to be in maintenance mode, with the last significant update in early 2023. While functional, active development might be slower compared to alternatives.","severity":"gotcha","affected_versions":"<=7.2.2"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Run `tsfmt -r <file>` to automatically format and replace the file, or modify your code/configuration to meet the expected format.","cause":"The content of the specified TypeScript file does not conform to the configured formatting rules.","error":"sample.ts is not formatted"},{"fix":"Install `typescript` as a direct dependency in your project: `npm install typescript` or `yarn add typescript`. Ensure the installed version satisfies the peer dependency range of `typescript-formatter`.","cause":"The `typescript` peer dependency is missing or cannot be resolved by the package manager.","error":"Cannot find module 'typescript'"},{"fix":"First, check the TypeScript file for any syntax errors. If the file is valid, ensure your `typescript` peer dependency is within the compatible range for `typescript-formatter` and consider upgrading `typescript-formatter` to its latest version.","cause":"An error occurred internally during the formatting process, often due to syntax errors in the input TypeScript code or an incompatibility with the TypeScript language service version.","error":"TypeError: [tsfmt] Formatting failed"}],"ecosystem":"npm"}