···149150## Input Format
151000152The `treeify` function accepts arrays with the following structure:
1531541. First element must be a string (the root node)
···149150## Input Format
151152+> **Disclaimer:**
153+> The exported `TreeInput` type (`Array<string | TreeInput>`) is intentionally flexible to support dynamic and programmatic tree construction. However, TypeScript cannot enforce at the type level that the first element is a string. This requirement is checked at runtime by the `treeify` function, which will throw an error if the first element is not a string. Please ensure your input arrays follow this convention.
154+155The `treeify` function accepts arrays with the following structure:
1561571. First element must be a string (the root node)
+10-10
src/index.ts
···1/**
2- * Represents a node in the tree structure.
3- * Can be either a string (a leaf node) or an array of TreeNodes (a branch with children).
4 */
5-type TreeNode = string | TreeNode[]
67/**
8- * The strict tree input format. Must start with a string.
9- * This type is exported for testing purposes and advanced usage.
10 */
11-export type TreeInput = [string, ...Array<string | TreeNode[]>] | string[]
1213/**
14 * Flexible input type that accepts any array.
···16 */
17type FlexibleTreeInput = readonly (string | unknown[])[]
1819-type TreeChars = {
00020 branch: string
21 lastBranch: string
22 pipe: string
23 space: string
24}
2526-/**
27- * @description ASCII characters used to render the tree.
28- */
29const DEFAULT_CHARS: TreeChars = {
30 branch: '├─ ',
31 lastBranch: '└─ ',
···1/**
2+ * The strict tree input format. Must start with a string.
3+ * This type is exported for testing purposes and advanced usage.
4 */
5+export type TreeInput = Array<string | TreeInput>
67/**
8+ * Represents a node in the tree structure.
9+ * Can be either a string (a leaf node) or an array of TreeNodes (a branch with children).
10 */
11+type TreeNode = string | TreeNode[]
1213/**
14 * Flexible input type that accepts any array.
···16 */
17type FlexibleTreeInput = readonly (string | unknown[])[]
1819+/**
20+ * ASCII characters used to render the tree.
21+ */
22+export type TreeChars = {
23 branch: string
24 lastBranch: string
25 pipe: string
26 space: string
27}
2800029const DEFAULT_CHARS: TreeChars = {
30 branch: '├─ ',
31 lastBranch: '└─ ',
+28-9
src/types.test.ts
···4import type { TreeInput } from './index.js'
56describe('treeify types', () => {
7- test('input with values', () => {
8 const inputWithValues: TreeInput = ['root', ['child1', 'child2', 'child3']]
9 let result = treeify(inputWithValues)
10 assert.ok(result)
1112- const inputWithoutValues: TreeInput = []
13- result = treeify(inputWithoutValues)
14 assert.equal(result, '') // empty string
15000000016 // @ts-expect-error
17 const inputWithoutRootString: TreeInput = [{ bad: 'root' }, 'root2']
18 assert.throws(() => treeify(inputWithoutRootString), {
19 message: 'First element must be a string',
20 })
21022 // @ts-expect-error
23- const inputWithRootStringAndInvalidValues: TreeInput = [
24- 'root',
25- 1,
26- {},
27- ['child1', 'child2', 'child3'],
28- ]
29 result = treeify(inputWithRootStringAndInvalidValues)
30 assert.ok(result) // non-strings are ignored
0000000000000031 })
32})