TypeScript Dedent

2.2.0 · maintenance · verified Sun Apr 19

ts-dedent is a TypeScript package designed to intelligently remove common leading indentation and trim leading/trailing empty lines from multi-line strings, primarily when used with ES6 template literals. It aims to make highly indented code blocks within strings, such as SQL queries, HTML, or code samples, readable in source code without affecting their runtime representation. The current stable version is 2.2.0, with releases occurring irregularly, typically driven by dependency updates or minor bug fixes rather than continuous feature development. Key differentiators include its TypeScript-first approach, smart handling of indentation (only removing common leading whitespace), and robust support for template literal placeholders, building on concepts from other dedent libraries while providing specific TypeScript advantages.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates `ts-dedent` usage as a template tag and a function, showing how it intelligently removes common indentation and handles placeholders in multi-line strings.

import dedent from 'ts-dedent';

// Example 1: Basic dedentation
console.log(dedent`
  A string that gets so long you need to break it over
  multiple lines. Luckily dedent is here to keep it
  readable without lots of spaces ending up in the string
  itself.
`);

// Example 2: Dedentation with nested indents and placeholders
const user = 'Alice';
const items = ['apple', 'banana', 'cherry'];
console.log(dedent`
  Hello, ${user}!
  Here is your shopping list:

    * ${items[0]}
    * ${items[1]}
    * ${items[2]}

  Thank you.
`);

// Example 3: Using dedent as a function
const multilineString = dedent(`
  This also works perfectly
  fine as a regular function call.
`);
console.log(multilineString);

view raw JSON →