For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt, and this page is available as Markdown at /reference/string/dedent.md.

dedent es-toolkit

Removes the common leading whitespace from every line of a multi-line string.

Use it to write multi-line strings that follow your code's indentation, without the indentation ending up in the actual string.

const text = dedent`
  Hello
  World
`;

Usage

dedent`text`

Use dedent as a tagged template literal to write multi-line strings inside indented code. It finds the smallest indentation shared by the non-empty lines and removes it from every line, so relative indentation differences between lines are preserved. If the first or last line contains only whitespace, it is removed.

import { dedent } from 'es-toolkit/string';

// The indentation from the code is removed
const message = dedent`
  Hello
  World
`;
// message is 'Hello\nWorld'

// Relative indentation between lines is preserved
const list = dedent`
  Items:
    - First
    - Second
`;
// list is 'Items:\n  - First\n  - Second'

// Interpolated values are inserted before the indentation is removed
const name = 'es-toolkit';
const greeting = dedent`
  Hello, ${name}!
`;
// greeting is 'Hello, es-toolkit!'

Lines that contain only whitespace become empty lines, and Windows line endings (\r\n) are normalized to \n.

import { dedent } from 'es-toolkit/string';

// Whitespace-only lines become empty lines
const text = dedent`
  First

  Second
`;
// text is 'First\n\nSecond'

Parameters

strTemplateStringsArrayrequired

The template literal to dedent.

valuesunknown[]required

The values to interpolate into the template literal.

Returns

The string with the common leading whitespace removed.

string

dedent(str)

Use dedent as a regular function when you want to remove the common leading whitespace from a string already stored in a variable.

import { dedent } from 'es-toolkit/string';

// Remove the common indentation from an existing string
const raw = '  Hello\n    World';
const text = dedent(raw);
// text is 'Hello\n  World'

Parameters

strstringrequired

The string to dedent.

Returns

The string with the common leading whitespace removed.

string

dedent(tagFn)

To combine dedent with another tag function, pass the tag function as an argument, like dedent(tagFn). The new tag function receives template strings with the common leading whitespace already removed.

import { dedent } from 'es-toolkit/string';

// A tag function that runs the Python code it receives
function pythonInterpreter(strings: TemplateStringsArray, ...values: unknown[]) {
  return runPython(strings.join(''));
}

// Wrapping it with dedent makes it receive dedented code
const python = dedent(pythonInterpreter);

python`
  def greet():
      print("Hello!")

  greet()
`;
// pythonInterpreter receives:
// 'def greet():\n    print("Hello!")\n\ngreet()'

Parameters

tagFn(strings: TemplateStringsArray, ...values: unknown[]) => Trequired

The tag function to compose.

Returns

A new tag function that removes the common leading whitespace from the template strings before passing them to tagFn.

(strings: TemplateStringsArray, ...values: unknown[]) => T
Source: es-toolkit

Re-exported verbatim from es-toolkit. Implementation, edge cases, and performance behavior are owned upstream. This page mirrors the documentation at the pinned version; the linked source is authoritative.