ianstormtaylor/slate · error · Error

When setting the selection and the current selection is \`nu

Error message

When setting the selection and the current selection is \`null\` you must provide at least an \`anchor\` and \`focus\`, but you passed: ${Scrubber.stringify(
        target
      )}

What it means

select() (Transforms.select) requires a full Range when the current editor selection is null, because there is nothing to derive the missing bound from. The target must be a complete Range with both anchor and focus (or a Point/Path which converts to one); a partial object like {anchor} alone is rejected.

Source

Thrown at packages/slate/src/transforms-selection/select.ts:17

import { SelectionTransforms } from '../interfaces/transforms/selection'
import { Editor } from '../interfaces/editor'
import { Transforms } from '../interfaces/transforms'
import { Scrubber } from '../interfaces/scrubber'
import { Location } from '../interfaces'

export const select: SelectionTransforms['select'] = (editor, target) => {
  const { selection } = editor
  target = Editor.range(editor, target)

  if (selection) {
    Transforms.setSelection(editor, target)
    return
  }

  if (!Location.isRange(target)) {
    throw new Error(
      `When setting the selection and the current selection is \`null\` you must provide at least an \`anchor\` and \`focus\`, but you passed: ${Scrubber.stringify(
        target
      )}`
    )
  }

  editor.apply({
    type: 'set_selection',
    properties: selection,
    newProperties: target,
  })
}

View on GitHub (pinned to 72a37c701e)

Solutions

  1. Pass a complete Range {anchor, focus}, or a Point/Path/Range object created via Range.isRange-valid data
  2. Use Point refs or Editor.point(editor, at) to build a valid full target
  3. If target may be partial, merge it with editor.selection when it exists, else require both bounds

Example fix

// before
Transforms.select(editor, { anchor }) // selection is null, focus missing

// after
Transforms.select(editor, { anchor, focus: anchor })
Defensive patterns

Strategy: type-guard

Validate before calling

if (editor.selection == null && !Range.isRange(target)) {
  throw new Error('Provide a full Range when selection is null')
}
Transforms.select(editor, target)

Type guard

const isSelectableTarget = (t: unknown): t is Range | Point =>
  Range.isRange(t) || Point.isPoint(t) || Path.isPath(t)

Prevention

When it happens

Trigger: Transforms.select(editor, { anchor: point }) with selection null; passing a partial Range-like object when the editor was never selected or after deselect(); passing a malformed target (string, number, or object missing anchor/focus).

Common situations: Restoring a saved selection from localStorage where focus was dropped; programmatic focus-after-mount before any user selection; spread of optional props producing an incomplete range.

Related errors


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