ianstormtaylor/slate · error · Error
Got non-numeric path index
Error message
Got non-numeric path index
What it means
Node.getIf() iterates over path segments and requires each segment to be a number. A segment was something else (string, undefined, null), which means the path is malformed rather than merely pointing at a missing node. Unlike Node.get, this error cannot be avoided by the usual existence check — it signals a type bug in the caller's path construction.
Source
Thrown at packages/slate/src/interfaces/node.ts:438
const node = Node.getIf(root, path)
if (node === undefined) {
throw new Error(
`Cannot find a descendant at path [${path}] in node: ${Scrubber.stringify(
root
)}`
)
}
return node
},
getIf(root: Node, path: Path): Node | undefined {
let node = root
for (let i = 0; i < path.length; i++) {
const p = path[i]
if (typeof p !== 'number') {
throw new Error('Got non-numeric path index')
}
if (Node.isText(node) || !node.children[p]) {
return
}
node = node.children[p]
}
return node
},
has(root: Node, path: Path): boolean {
let node = root
for (let i = 0; i < path.length; i++) {
const p = path[i]
View on GitHub (pinned to 72a37c701e)
Solutions
- Validate/normalize the path to numbers before use: path.every(p => typeof p === 'number') or map with Number().
- Type paths explicitly as Path (number[]) instead of any/string[] so the compiler catches bad construction.
- When ingesting external paths, sanitize: const p = raw.map(Number).filter(n => Number.isInteger(n)).
- Fix the source of the path: usually string concatenation ('' + index) or optional chaining producing undefined.
Example fix
// before const path = [String(index), 0] // oops: string segment const node = Node.getIf(editor, path) // after const path = [index, 0] const node = Node.getIf(editor, path as Path)
Defensive patterns
Strategy: type-guard
Validate before calling
if (!path.every(p => Number.isInteger(p))) { /* reject/normalize path */ } Type guard
const isPath = (p: unknown): p is Path => Array.isArray(p) && p.every(n => Number.isInteger(n))
Prevention
- Type paths as Path, never any[].
- Convert serialized paths with .map(Number) before use.
When it happens
Trigger: Passing a path built by spreading a string or mixed array like ['0', 1]; a path containing undefined due to array holes ([ , 0]); JSON-parsed paths where a segment became a string; concatenating a path with a non-numeric value.
Common situations: Serializing/deserializing paths as strings and forgetting to convert back to numbers; building paths from untyped external data (URL params, localStorage, server payloads) without validation; TypeScript any-typed path plumbing erasing safety.
Related errors
- Got non-numeric path index
- Cannot get the relative path of [${path}] inside ancestor [$
- Unable to find the path for Slate node: ${Scrubber.stringify
- Cannot get the next node from the root node!
- Cannot get the previous node from the root node!
AI-assisted analysis of ianstormtaylor/slate@72a37c701e (2026-08-27).
Data as JSON: /api/errors/40c2ccf16b61d04c.
Report an issue: GitHub.