A heredoc implementation in TypeScript with support for nested heredocs.
npm install heredoc-tsA TypeScript heredoc implementation with smart indentation handling and
interpolation support.
``bash`
npm install heredoc-ts
`typescript
import { heredoc } from "heredoc-ts"
const message = heredoc
Hello, world!
This is a multiline string
with proper indentation handling.
console.log(message)
// "Hello, world!\nThis is a multiline string\nwith proper indentation handling."
`
The heredoc function automatically detects and removes the common leading
indentation from all lines:
`typescript
const code = heredoc
function example() {
console.log("Hello world")
}function example() {\n console.log("Hello world")\n}
// `
Supports template literal interpolation with intelligent handling of multiline
values. This automatically handles the leading spacing (i.e., each newline in
the template value has the leading spacing prepended).
`typescript
const items = ["apple", "banana", "orange"]
const list = heredoc
Shopping list:
${items.map((item) => - ${item}).join("\n")}`
// "Shopping list:\n- apple\n- banana\n- orange"
You can nest heredoc calls for complex formatting. This will process the
indentation of the nested value first, allowing for indented code for
readability while maintaining consistent outputs without excessive leading
spaces.
`typescript
const nested = heredoc
Outer content
${heredoc}
Inner content
with its own
indentation
Back to outer`
// "Outer content\nInner content\nwith its own\nindentation\nBack to outer"
Fully typed with TypeScript, supporting string, number, boolean, null, and
undefined interpolations.
A template tag function that processes multiline strings:
- Removes leading and trailing blank lines
- Detects the smallest common indentation and strips it from all lines
- Preserves relative indentation between lines
- Intelligently handles interpolated values with newlines
`typescript
const text = heredoc
This text starts with proper indentation.
It maintains relative indentation.
Like this indented line.`
`typescript
const name = "Alice"
const greeting = heredoc
Hello, ${name}!
Welcome to our application.`
`typescript
const data = ["one", "two", "three"]
const html = heredoc
- ${item}
).join("\n")}
`Development
`bash
Install dependencies
pnpm installRun tests
pnpm testBuild the package
pnpm buildRun linting
pnpm lintType checking
pnpm typecheck
`License
Copyright © 2025 Corey Ward. Available under the MIT license.
Credits
This package was originally based on the now-archived package
theredoc` by Justin Searls. This