Repository navigation
[lexical][lexical-table] Breaking change: Match the Flow declarations of UpdateListenerPayload, afterCloneFrom and getTableObserverFromTableElement to TypeScript - #10064
Conversation
… 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.`
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
AI review status for this pull request.
|
lexical
@lexical/a11y
@lexical/clipboard
@lexical/code
@lexical/code-core
@lexical/code-prism
@lexical/code-shiki
@lexical/compiler
@lexical/devtools-core
@lexical/dragon
@lexical/eslint-plugin
@lexical/extension
@lexical/fast-check
@lexical/file
@lexical/hashtag
@lexical/headless
@lexical/history
@lexical/html
@lexical/internal
@lexical/link
@lexical/list
@lexical/mark
@lexical/markdown
@lexical/mdast
@lexical/offset
@lexical/overflow
@lexical/plain-text
@lexical/react
@lexical/rich-text
@lexical/selection
@lexical/table
@lexical/tailwind
@lexical/text
@lexical/utils
@lexical/y
@lexical/yjs
commit: |
There was a problem hiding this comment.
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:
getTableElement's result no longer type checks as an argument togetTableObserverFromTableElementin Flow.getTableElementis declared as returningHTMLTableElementWithWithTableSelectionState | (T & null)(packages/lexical-table/flow/LexicalTable.js.flow:204-207). TypeScript reducesHTMLElement & nulltonever, but Flow keeps it, sogetTableObserverFromTableElement(getTableElement(tableNode, dom))withdom: HTMLElementpasses in TypeScript and against the old Flow declaration, and now fails withEither 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:It also fixes the null-checked form (declare export function getTableElement( tableNode: TableNode, dom: HTMLElement, ): HTMLTableElementWithWithTableSelectionState; declare export function getTableElement( tableNode: TableNode, dom: null | HTMLElement, ): null | HTMLTableElementWithWithTableSelectionState;
getTableElement(tableNode, editor.getElementByKey(key))followed by!== null), which already fails onmainwithinvalid-compare. If you'd rather keep this PR to the three declarations, the "Type signature changes" section of.changeset/README.mdasksDetect:to name each call shape that stops compiling, so listing this call there would cover it.Detect:could also name exact-object consumers of the payload. WithUpdateListenerPayloadnow 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 reportsinexact UpdateListenerPayload [1] is incompatible with exact ... [incompatible-exact]. ADetect:line for that shape, and aMigration:note to make the receiving type inexact with..., would complete the entry per the same README section.- A committed guard would keep these in sync.
packages/lexical/flow/Lexical.flowtest.js.flowexists to use the declared API the way a downstream Flow app does; adding the passing forms from your test plan (readingmutatedNodes, anafterCloneFrom(prevNode: this)override that callssuper, aninstanceof HTMLTableElementcall) would letpnpm run flowcatch a regression. resetOnCopyNodeFromstill carries the old rationale (nit). Just below the changed line,resetOnCopyNodeFrom(original: any)(packages/lexical/flow/Lexical.js.flow:655-656) keeps the comment sayingthisis rejected by Flow's variance model, while TypeScript declaresresetOnCopyNodeFrom(originalNode: this). This PR shows the suppressedthisform works, so it could be a candidate for the next PR in the series.
| declare export function getTableObserverFromTableElement( | ||
| tableElement: HTMLElement, | ||
| ): null | TableObserver; | ||
| tableElement: HTMLTableElementWithWithTableSelectionState, |
There was a problem hiding this comment.
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`). |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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).
Description
Three Flow declarations differ from TypeScript:
UpdateListenerPayloadlacks TypeScript'smutatedNodes: null | MutatedNodesfield, and it is an exact object type where TypeScript declares an interface.LexicalNode.afterCloneFromtakesprevNode: any. TypeScript declaresafterCloneFrom(prevNode: this): void.getTableObserverFromTableElementtakes anHTMLElement. TypeScript takes anHTMLTableElementWithWithTableSelectionState.This change makes all three match TypeScript.
UpdateListenerPayloadlists TypeScript's seven fields in TypeScript's order, as an inexact object type. Flow does not acceptthisin a parameter, so theafterCloneFromdeclaration carries a$FlowFixMe[incompatible-variance].Changeset
.changeset/flow-update-payload-clone-table-observer.mdis[breaking](minor) forlexicaland@lexical/table. Three kinds of Flow code stop type checking:UpdateListenerPayloadwithoutmutatedNodes;afterCloneFromcall with a node of another type, such assuper.afterCloneFrom(prevNode)in an override typedprevNode: MyNode;getTableObserverFromTableElementcall with anHTMLElementthat is not known to be anHTMLTableElement.Test plan
I ran
flow full-checkwith the repo's.flowconfigon this file:Before
After
Reading
mutatedNodestype checks. The cases the changeset'sDetect:describes report:The changeset's
Migration:forms type check: a payload withmutatedNodes: null, an element narrowed withinstanceof HTMLTableElement, and an override typedafterCloneFrom(prevNode: this)with// $FlowFixMe[incompatible-variance]that callssuper.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 reportsCannot 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.