ianstormtaylor/slate · error · Error

Cannot remove the "${key}" selection property

Error message

Cannot remove the "${key}" selection property

What it means

Thrown when a set_selection operation tries to remove the anchor or focus property by setting it to null/undefined. A Slate selection cannot exist without anchor and focus points, so removing either is invalid.

Source

Thrown at packages/slate/src/interfaces/transforms/general.ts:335

              `Cannot set the "${key}" property of the selection!`
            )
          }

          const value = newProperties[<keyof Range>key]

          // Make sure we're not setting `then` to a function, since this will
          // cause the selection to be treated as a Promise-like object, which
          // can cause unexpected behaviour when returning the selection from
          // async functions.
          if (key === 'then' && typeof value === 'function') {
            throw new Error(
              'Cannot set the "then" property of the selection to a function'
            )
          }

          if (value == null) {
            if (key === 'anchor' || key === 'focus') {
              throw new Error(`Cannot remove the "${key}" selection property`)
            }

            delete selection[<keyof Range>key]
          } else {
            selection[<keyof Range>key] = value
          }
        }

        editor.selection = selection

        break
      }

      case 'split_node': {
        const { path, position, properties } = op
        const index = path[path.length - 1]

        if (path.length === 0) {

View on GitHub (pinned to 72a37c701e)

Solutions

  1. Use editor.select(null) / Transforms.deselect(editor) to clear the whole selection instead of nulling anchor/focus
  2. Filter out null/undefined anchor and focus before calling setSelection
  3. Ensure the properties object only contains keys you intend to change with non-null point values

Example fix

// before
Transforms.setSelection(editor, { focus: maybePoint }) // maybePoint may be undefined

// after
if (maybePoint) {
  Transforms.setSelection(editor, { focus: maybePoint })
} else {
  Transforms.deselect(editor)
}
Defensive patterns

Strategy: validation

Validate before calling

const clean = Object.fromEntries(
  Object.entries(props).filter(
    ([k, v]) => !(v == null && (k === 'anchor' || k === 'focus'))
  )
)
if (clean.anchor == null && clean.focus == null && Object.keys(clean).length === 0) {
  Transforms.deselect(editor)
} else {
  Transforms.setSelection(editor, clean)
}

Prevention

When it happens

Trigger: Transforms.setSelection(editor, { anchor: null }) or { focus: undefined }; remote ops setting anchor/focus to null; passing a partial object built from spread of an object with nullish anchor/focus.

Common situations: Merging optional props that can be undefined into selection updates; collaborative op replay where a peer cleared a point; incorrect construction like {...(cond && {anchor})} producing explicit undefined keys.

Related errors


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