ianstormtaylor/slate · error · Error

Got non-numeric path index

Error message

Got non-numeric path index

What it means

Path.common() computes the shared prefix of two paths by pairwise comparing segments; every segment must be a number. One of the paths contained a non-numeric segment (string, undefined, etc.), so the comparison cannot proceed.

Source

Thrown at packages/slate/src/interfaces/path.ts:203

    if (reverse) {
      paths = paths.slice(1)
    } else {
      paths = paths.slice(0, -1)
    }

    return paths
  },

  common(path: Path, another: Path): Path {
    const common: Path = []

    for (let i = 0; i < path.length && i < another.length; i++) {
      const av = path[i]
      const bv = another[i]

      if (typeof av !== 'number' || typeof bv !== 'number') {
        throw new Error('Got non-numeric path index')
      }

      if (av !== bv) {
        break
      }

      common.push(av)
    }

    return common
  },

  compare(path: Path, another: Path): -1 | 0 | 1 {
    const min = Math.min(path.length, another.length)

    for (let i = 0; i < min; i++) {
      if (path[i] < another[i]) return -1
      if (path[i] > another[i]) return 1

View on GitHub (pinned to 72a37c701e)

Solutions

  1. Coerce external paths: const p = raw.split('.').map(Number) before use.
  2. Type both arguments as Path and construct paths only via Path APIs or numeric array operations.
  3. Add a runtime guard: path.every(n => Number.isInteger(n)) before calling Path.common.
  4. Fix upstream bugs where an index is computed with string ops or where optional access yields undefined.

Example fix

// before
const common = Path.common(pathA.split('.'), pathB) // string segments!

// after
const common = Path.common(pathA.split('.').map(Number) as Path, pathB)
Defensive patterns

Strategy: type-guard

Validate before calling

const ok = [a, b].every(p => p.every(n => Number.isInteger(n)))
if (!ok) { /* reject */ }

Type guard

const isPath = (p: unknown): p is Path => Array.isArray(p) && p.every(n => Number.isInteger(n))

Prevention

When it happens

Trigger: Calling Path.common(pathA, pathB) where either array contains a string index, undefined, or null; paths assembled from serialized strings or unvalidated external data; one path being undefined itself and index access yielding undefined.

Common situations: Persisting/loading paths (localStorage, server, URL) as strings like '0.1' and splitting without converting to numbers; mixing path types after JSON round-trips; a variable typed any shadowing a real Path.

Related errors


AI-assisted analysis of ianstormtaylor/slate@72a37c701e (2026-08-27). Data as JSON: /api/errors/00910fb35b11f929. Report an issue: GitHub.