> For AI agents: the complete documentation index is available at https://massaman.dev/llms.txt, the full documentation bundle is available at https://massaman.dev/llms-full.txt.

# 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.

```typescript
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.

```typescript
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`.

```typescript
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.

```typescript
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.

```typescript
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.

```typescript
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.

```typescript
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`.

```typescript
(strings: TemplateStringsArray, ...values: unknown[]) => T
```


Source: es-toolkit

Re-exported verbatim from [`es-toolkit`](https://es-toolkit.dev/reference/string/dedent.html). Implementation, edge cases, and performance behavior are owned upstream. This page mirrors the documentation at the pinned version; the linked source is authoritative.
