Skip to content

[lexical][lexical-table] Breaking change: Match the Flow declarations of UpdateListenerPayload, afterCloneFrom and getTableObserverFromTableElement to TypeScript - #10064

Merged
SamChou19815 merged 1 commit into
meta:mainfrom
SamChou19815:flow-update-payload-clone-table-observer
Dec 6, 2026

Conversation

@SamChou19815

Copy link
Copy Markdown
Contributor

Description

Three Flow declarations differ from TypeScript:

  • UpdateListenerPayload lacks TypeScript's mutatedNodes: null | MutatedNodes field, and it is an exact object type where TypeScript declares an interface.
  • LexicalNode.afterCloneFrom takes prevNode: any. TypeScript declares afterCloneFrom(prevNode: this): void.
  • getTableObserverFromTableElement takes an HTMLElement. TypeScript takes an HTMLTableElementWithWithTableSelectionState.

This change makes all three match TypeScript. UpdateListenerPayload lists TypeScript's seven fields in TypeScript's order, as an inexact object type. Flow does not accept this in a parameter, so the afterCloneFrom declaration carries a $FlowFixMe[incompatible-variance].

Changeset

.changeset/flow-update-payload-clone-table-observer.md is [breaking] (minor) for lexical and @lexical/table. Three kinds of Flow code stop type checking:

  • a hand-built UpdateListenerPayload without mutatedNodes;
  • an afterCloneFrom call with a node of another type, such as super.afterCloneFrom(prevNode) in an override typed prevNode: MyNode;
  • a getTableObserverFromTableElement call with an HTMLElement that is not known to be an HTMLTableElement.

Test plan

I ran flow full-check with the repo's .flowconfig on this file:

import type {LexicalEditor, LexicalNode, NodeMutation, UpdateListenerPayload} from 'lexical';
import type {HTMLTableElementWithWithTableSelectionState} from '@lexical/table';
import {TextNode} from 'lexical';
import {getTableObserverFromTableElement} from '@lexical/table';

declare const editor: LexicalEditor;
editor.registerUpdateListener(payload => {
  const mutated = payload.mutatedNodes;
  if (mutated != null) {
    for (const [klass, byKey] of mutated) {
      const m: ?NodeMutation = byKey.get('1');
    }
  }
});
declare const prev: UpdateListenerPayload;
const handBuilt: UpdateListenerPayload = {
  dirtyElements: new Map(),
  dirtyLeaves: new Set(),
  editorState: prev.editorState,
  normalizedNodes: new Set(),
  prevEditorState: prev.prevEditorState,
  tags: new Set(),
};

declare const tableElement: HTMLTableElementWithWithTableSelectionState;
declare const anyElement: HTMLElement;
getTableObserverFromTableElement(tableElement);
getTableObserverFromTableElement(anyElement);

class NamedText extends TextNode {
  afterCloneFrom(prevNode: NamedText): void {
    super.afterCloneFrom(prevNode);
  }
}
declare const a: TextNode;
declare const b: LexicalNode;
a.afterCloneFrom(a);
a.afterCloneFrom(b);

Before

Cannot get `payload.mutatedNodes` because property `mutatedNodes` is missing in `UpdateListenerPayload` [1].
[prop-missing]

After

Reading mutatedNodes type checks. The cases the changeset's Detect: describes report:

Cannot assign object literal to `handBuilt` because property `mutatedNodes` is missing in object literal [1] but exists
in `UpdateListenerPayload` [2]. [incompatible-type]
Cannot call `getTableObserverFromTableElement` with `anyElement` bound to `tableElement` because `HTMLElement` [1] is
incompatible with `HTMLTableElement` [2]. For example, property `align` is missing in `HTMLElement` [1] but exists in
`HTMLTableElement` [2]. [incompatible-type]
Cannot call `super.afterCloneFrom` with `prevNode` bound to `prevNode` because `NamedText` [1] is incompatible with
`empty` [2]. [incompatible-type]
Cannot call `a.afterCloneFrom` with `b` bound to `prevNode` because `LexicalNode` [1] is incompatible with
`TextNode` [2]. [incompatible-type]

The changeset's Migration: forms type check: a payload with mutatedNodes: null, an element narrowed with instanceof HTMLTableElement, and an override typed afterCloneFrom(prevNode: this) with // $FlowFixMe[incompatible-variance] that calls super.afterCloneFrom(prevNode).

Repo checks:

  • pnpm run flow: Found 0 errors. No Flow file or flowtest in the repo needed a change. Without the $FlowFixMe, Flow reports Cannot use this [1] in an input position because this [1] is expected to occur only in output positions. [incompatible-variance].
  • pnpm run lint-flow: the same 265 warnings as before.
  • pnpm run prettier: clean.
  • pnpm run check-changesets: Changeset check passed: 18 pending changeset(s) valid.

… of UpdateListenerPayload, afterCloneFrom and getTableObserverFromTableElement to TypeScript

## Description

Three Flow declarations differ from TypeScript:
- `UpdateListenerPayload` lacks TypeScript's `mutatedNodes: null | MutatedNodes` field, and it is an exact object type where TypeScript declares an interface.
- `LexicalNode.afterCloneFrom` takes `prevNode: any`. TypeScript declares `afterCloneFrom(prevNode: this): void`.
- `getTableObserverFromTableElement` takes an `HTMLElement`. TypeScript takes an `HTMLTableElementWithWithTableSelectionState`.

This change makes all three match TypeScript. `UpdateListenerPayload` lists TypeScript's seven fields in TypeScript's order, as an inexact object type. Flow does not accept `this` in a parameter, so the `afterCloneFrom` declaration carries a `$FlowFixMe[incompatible-variance]`.

## Changeset

`.changeset/flow-update-payload-clone-table-observer.md` is `[breaking]` (minor) for `lexical` and `@lexical/table`. Three kinds of Flow code stop type checking:
- a hand-built `UpdateListenerPayload` without `mutatedNodes`;
- an `afterCloneFrom` call with a node of another type, such as `super.afterCloneFrom(prevNode)` in an override typed `prevNode: MyNode`;
- a `getTableObserverFromTableElement` call with an `HTMLElement` that is not known to be an `HTMLTableElement`.

## Test plan

I ran `flow full-check` with the repo's `.flowconfig` on this file:

```js
import type {LexicalEditor, LexicalNode, NodeMutation, UpdateListenerPayload} from 'lexical';
import type {HTMLTableElementWithWithTableSelectionState} from '@lexical/table';
import {TextNode} from 'lexical';
import {getTableObserverFromTableElement} from '@lexical/table';

declare const editor: LexicalEditor;
editor.registerUpdateListener(payload => {
  const mutated = payload.mutatedNodes;
  if (mutated != null) {
    for (const [klass, byKey] of mutated) {
      const m: ?NodeMutation = byKey.get('1');
    }
  }
});
declare const prev: UpdateListenerPayload;
const handBuilt: UpdateListenerPayload = {
  dirtyElements: new Map(),
  dirtyLeaves: new Set(),
  editorState: prev.editorState,
  normalizedNodes: new Set(),
  prevEditorState: prev.prevEditorState,
  tags: new Set(),
};

declare const tableElement: HTMLTableElementWithWithTableSelectionState;
declare const anyElement: HTMLElement;
getTableObserverFromTableElement(tableElement);
getTableObserverFromTableElement(anyElement);

class NamedText extends TextNode {
  afterCloneFrom(prevNode: NamedText): void {
    super.afterCloneFrom(prevNode);
  }
}
declare const a: TextNode;
declare const b: LexicalNode;
a.afterCloneFrom(a);
a.afterCloneFrom(b);
```

### Before

```
Cannot get `payload.mutatedNodes` because property `mutatedNodes` is missing in `UpdateListenerPayload` [1].
[prop-missing]
```

### After

Reading `mutatedNodes` type checks. The cases the changeset's `Detect:` describes report:

```
Cannot assign object literal to `handBuilt` because property `mutatedNodes` is missing in object literal [1] but exists
in `UpdateListenerPayload` [2]. [incompatible-type]
Cannot call `getTableObserverFromTableElement` with `anyElement` bound to `tableElement` because `HTMLElement` [1] is
incompatible with `HTMLTableElement` [2]. For example, property `align` is missing in `HTMLElement` [1] but exists in
`HTMLTableElement` [2]. [incompatible-type]
Cannot call `super.afterCloneFrom` with `prevNode` bound to `prevNode` because `NamedText` [1] is incompatible with
`empty` [2]. [incompatible-type]
Cannot call `a.afterCloneFrom` with `b` bound to `prevNode` because `LexicalNode` [1] is incompatible with
`TextNode` [2]. [incompatible-type]
```

The changeset's `Migration:` forms type check: a payload with `mutatedNodes: null`, an element narrowed with `instanceof HTMLTableElement`, and an override typed `afterCloneFrom(prevNode: this)` with `// $FlowFixMe[incompatible-variance]` that calls `super.afterCloneFrom(prevNode)`.

Repo checks:
- `pnpm run flow`: `Found 0 errors`. No Flow file or flowtest in the repo needed a change. Without the `$FlowFixMe`, Flow reports `Cannot use this [1] in an input position because this [1] is expected to occur only in output positions. [incompatible-variance]`.
- `pnpm run lint-flow`: the same 265 warnings as before.
- `pnpm run prettier`: clean.
- `pnpm run check-changesets`: `Changeset check passed: 18 pending changeset(s) valid.`
@vercel

vercel Bot commented Dec 6, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
lexical Ready Ready Preview Dec 6, 2026 3:22pm UTC
lexical-playground Ready Ready Preview Dec 6, 2026 3:22pm UTC

Request Review

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Dec 6, 2026
@rootnode-bot

rootnode-bot Bot commented Dec 6, 2026 •

Copy link
Copy Markdown

AI review status for this pull request.

Review status Updated
🟢 Reviewed (for maintainers only) Dec 6, 2026, 3:39 PM UTC

@pkg-pr-new

pkg-pr-new Bot commented Dec 6, 2026

Copy link
Copy Markdown
lexical

npm i https://pkg.pr.new/lexical@10064

@lexical/a11y

npm i https://pkg.pr.new/@lexical/a11y@10064

@lexical/clipboard

npm i https://pkg.pr.new/@lexical/clipboard@10064

@lexical/code

npm i https://pkg.pr.new/@lexical/code@10064

@lexical/code-core

npm i https://pkg.pr.new/@lexical/code-core@10064

@lexical/code-prism

npm i https://pkg.pr.new/@lexical/code-prism@10064

@lexical/code-shiki

npm i https://pkg.pr.new/@lexical/code-shiki@10064

@lexical/compiler

npm i https://pkg.pr.new/@lexical/compiler@10064

@lexical/devtools-core

npm i https://pkg.pr.new/@lexical/devtools-core@10064

@lexical/dragon

npm i https://pkg.pr.new/@lexical/dragon@10064

@lexical/eslint-plugin

npm i https://pkg.pr.new/@lexical/eslint-plugin@10064

@lexical/extension

npm i https://pkg.pr.new/@lexical/extension@10064

@lexical/fast-check

npm i https://pkg.pr.new/@lexical/fast-check@10064

@lexical/file

npm i https://pkg.pr.new/@lexical/file@10064

@lexical/hashtag

npm i https://pkg.pr.new/@lexical/hashtag@10064

@lexical/headless

npm i https://pkg.pr.new/@lexical/headless@10064

@lexical/history

npm i https://pkg.pr.new/@lexical/history@10064

@lexical/html

npm i https://pkg.pr.new/@lexical/html@10064

@lexical/internal

npm i https://pkg.pr.new/@lexical/internal@10064

@lexical/link

npm i https://pkg.pr.new/@lexical/link@10064

@lexical/list

npm i https://pkg.pr.new/@lexical/list@10064

@lexical/mark

npm i https://pkg.pr.new/@lexical/mark@10064

@lexical/markdown

npm i https://pkg.pr.new/@lexical/markdown@10064

@lexical/mdast

npm i https://pkg.pr.new/@lexical/mdast@10064

@lexical/offset

npm i https://pkg.pr.new/@lexical/offset@10064

@lexical/overflow

npm i https://pkg.pr.new/@lexical/overflow@10064

@lexical/plain-text

npm i https://pkg.pr.new/@lexical/plain-text@10064

@lexical/react

npm i https://pkg.pr.new/@lexical/react@10064

@lexical/rich-text

npm i https://pkg.pr.new/@lexical/rich-text@10064

@lexical/selection

npm i https://pkg.pr.new/@lexical/selection@10064

@lexical/table

npm i https://pkg.pr.new/@lexical/table@10064

@lexical/tailwind

npm i https://pkg.pr.new/@lexical/tailwind@10064

@lexical/text

npm i https://pkg.pr.new/@lexical/text@10064

@lexical/utils

npm i https://pkg.pr.new/@lexical/utils@10064

@lexical/y

npm i https://pkg.pr.new/@lexical/y@10064

@lexical/yjs

npm i https://pkg.pr.new/@lexical/yjs@10064

commit: 091c567

@rootnode-bot rootnode-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good, thanks for keeping the series going. The three Flow declarations now match their TypeScript counterparts, the changeset follows .changeset/README.md ([breaking] at minor, with Detect: and Migration:), and your test plan's Before/After output reproduces locally, including each Migration: form type checking.

A few optional, non-blocking suggestions:

  1. getTableElement's result no longer type checks as an argument to getTableObserverFromTableElement in Flow. getTableElement is declared as returning HTMLTableElementWithWithTableSelectionState | (T & null) (packages/lexical-table/flow/LexicalTable.js.flow:204-207). TypeScript reduces HTMLElement & null to never, but Flow keeps it, so getTableObserverFromTableElement(getTableElement(tableNode, dom)) with dom: HTMLElement passes in TypeScript and against the old Flow declaration, and now fails with Either property align is missing in HTMLElement [1] but exists in HTMLTableElement [2]. Or null [3] is incompatible with HTMLTableElement [2]. Replacing the generic with an overload pair fixed it locally:
    declare export function getTableElement(
      tableNode: TableNode,
      dom: HTMLElement,
    ): HTMLTableElementWithWithTableSelectionState;
    declare export function getTableElement(
      tableNode: TableNode,
      dom: null | HTMLElement,
    ): null | HTMLTableElementWithWithTableSelectionState;
    It also fixes the null-checked form (getTableElement(tableNode, editor.getElementByKey(key)) followed by !== null), which already fails on main with invalid-compare. If you'd rather keep this PR to the three declarations, the "Type signature changes" section of .changeset/README.md asks Detect: to name each call shape that stops compiling, so listing this call there would cover it.
  2. Detect: could also name exact-object consumers of the payload. With UpdateListenerPayload now inexact, Flow code that passes it where an exact object type is expected (a helper typed to return the six-field shape, or a listener parameter annotated with it) now reports inexact UpdateListenerPayload [1] is incompatible with exact ... [incompatible-exact]. A Detect: line for that shape, and a Migration: note to make the receiving type inexact with ..., would complete the entry per the same README section.
  3. A committed guard would keep these in sync. packages/lexical/flow/Lexical.flowtest.js.flow exists to use the declared API the way a downstream Flow app does; adding the passing forms from your test plan (reading mutatedNodes, an afterCloneFrom(prevNode: this) override that calls super, an instanceof HTMLTableElement call) would let pnpm run flow catch a regression.
  4. resetOnCopyNodeFrom still carries the old rationale (nit). Just below the changed line, resetOnCopyNodeFrom(original: any) (packages/lexical/flow/Lexical.js.flow:655-656) keeps the comment saying this is rejected by Flow's variance model, while TypeScript declares resetOnCopyNodeFrom(originalNode: this). This PR shows the suppressed this form works, so it could be a candidate for the next PR in the series.

Automated review (maintainer link)

declare export function getTableObserverFromTableElement(
tableElement: HTMLElement,
): null | TableObserver;
tableElement: HTMLTableElementWithWithTableSelectionState,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional: with this parameter type, getTableObserverFromTableElement(getTableElement(tableNode, dom)) (with dom: HTMLElement) stops type checking in Flow, though TypeScript accepts it: Flow keeps the T & null branch of getTableElement's declared return (line 207), which TypeScript reduces to never. An overload pair for getTableElement (dom: HTMLElement returning the table type, dom: null | HTMLElement returning null | the table type) fixed it locally; otherwise this call could go in Detect:.


`UpdateListenerPayload` has TypeScript's `mutatedNodes: null | MutatedNodes` field and is inexact, as a TypeScript interface is; `afterCloneFrom(prevNode: this)` replaces `afterCloneFrom(prevNode: any)`; and `getTableObserverFromTableElement` takes an `HTMLTableElementWithWithTableSelectionState`.

Detect: Flow code that builds an `UpdateListenerPayload` without `mutatedNodes` (`incompatible-type`: property `mutatedNodes` is missing); calls of `afterCloneFrom` with a node whose type is not the receiver's own, such as `super.afterCloneFrom(prevNode)` in an override typed `afterCloneFrom(prevNode: MyNode)` (`incompatible-type`); and calls of `getTableObserverFromTableElement` with an `HTMLElement` that is not known to be an `HTMLTableElement` (`incompatible-type`).

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional: since the payload is now inexact, Flow code that passes an UpdateListenerPayload where an exact object type is expected now reports incompatible-exact. Worth naming here, with "make the receiving type inexact" under Migration:, as the "Type signature changes" section of .changeset/README.md asks.

afterCloneFrom(prevNode: any): void;
// $FlowFixMe[incompatible-variance]
afterCloneFrom(prevNode: this): void;
// $FlowFixMe[unclear-type] -- Required to be `this`, which Flow's variance model rejects

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit, for a later PR: this rationale no longer holds now that afterCloneFrom above uses this under $FlowFixMe[incompatible-variance], and TypeScript declares resetOnCopyNodeFrom(originalNode: this).

@SamChou19815
SamChou19815 added this pull request to the merge queue Dec 6, 2026
Merged via the queue into meta:main with commit a8ba919 Dec 6, 2026
53 of 54 checks passed
@SamChou19815
SamChou19815 deleted the flow-update-payload-clone-table-observer branch December 6, 2026 16:14

This branch was successfully deployed

2 active deployments
Preview – lexical — 091c5670 Deployed Dec 6, 2026 by vercel[bot]
Preview – lexical-playground — 091c5670 Deployed Dec 6, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant