TypeScript Dedent
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
-
TypeError: (0 , ts_dedent_1.default) is not a function
cause Attempting to use `dedent` as a named import (e.g., `import { dedent } from 'ts-dedent';`) when it's exported as a default.fixChange the import statement to `import dedent from 'ts-dedent';`. -
TypeError: require(...) is not a function
cause Attempting to use CommonJS `require('ts-dedent')` directly without accessing the default export.fixModify the require statement to `const dedent = require('ts-dedent').default;`. -
My string has extra unwanted indentation after upgrading!
cause Upgrading to v2.0.0 changed how `ts-dedent` handles lines not starting with whitespace, causing previous assumptions about indentation removal to be incorrect.fixReview affected template literals. If a line should have its indentation removed, ensure it starts with whitespace, or adjust your expected output.
Warnings
- breaking Version 2.0.0 introduced a breaking change regarding indentation removal logic. Previously, indentation was always removed. Now, if a line does not start with whitespace, its indentation will not be removed.
- gotcha The README examples incorrectly show `import dedent from 'dedent';`. This refers to a *different* npm package. For `ts-dedent`, you *must* import from `'ts-dedent'`.
- deprecated Older versions (prior to 2.2.0) had less robust ESM support. While CJS was always supported, the introduction of a dedicated ESM module in v2.2.0 improved compatibility.
- gotcha Starting with v2.2.0, `ts-dedent` adds indentation to values that contain multiline strings. This might subtly change output if your placeholders previously contained un-indented multi-line strings.
Install
-
npm install ts-dedent -
yarn add ts-dedent -
pnpm add ts-dedent
Imports
- dedent
import { dedent } from 'ts-dedent';import dedent from 'ts-dedent';
- dedent
const dedent = require('ts-dedent');const dedent = require('ts-dedent').default; - dedent
import dedent from 'dedent';
import dedent from 'ts-dedent';
Quickstart
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);