[lexical][*] Chore: stop using deprecated traversal type parameters in tests#8667
Conversation
…acebook#8661 follow-up) Migrate the remaining call sites (all in tests) off the deprecated unsafe type parameters that facebook#8661 added to the node traversal methods (getFirstChild<T>(), getParent<T>(), getChildAtIndex<T>(), $getNodeByKey<T>(), etc.). The deprecated overloads themselves are retained for backwards compatibility with consumers. Add a $assertNodeType test helper that narrows a node via a type guard instead of an unchecked cast, and use it (alongside the existing $is* guards) in place of the deprecated type parameters and a few remaining unsafe `as` casts in the affected tests.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
potatowagon
left a comment
There was a problem hiding this comment.
Reviewed by Navi (Tater Thoughts Bobblehead) on behalf of @potatowagon.
Assessment: LGTM ✅
This is a well-scoped refactor that replaces deprecated generic type parameters on traversal methods (e.g. getFirstChildOrThrow<T>(), getFirstChild<T>(), getChildAtIndex<T>()) with a new runtime $assertNodeType() utility that uses type guards for type-safe narrowing.
What I checked:
-
The
$assertNodeTypeutility (packages/lexical/src/__tests__/utils/index.tsx): Clean implementation — takes anode | null | undefinedand a type guard, throws a clear error if the guard fails. Returns the narrowed type. This is strictly better than the previousas T/!pattern because it provides a runtime assertion rather than silently producing undefined behavior on type mismatch. -
Consistency of the migration pattern across all 21 test files: Every change follows the same mechanical pattern —
getFirstChildOrThrow<Foo>()→$assertNodeType(getFirstChild(), $isFoo), andgetChildAtIndex<Foo>(n)!→$assertNodeType(getChildAtIndex(n), $isFoo). No logic changes, just type narrowing approach. -
Import cleanup: Removed unused concrete type imports (e.g.
ParagraphNode,TextNode,TableNode) and added the corresponding type guard imports ($isParagraphNode,$isTextNode,$isTableNode). This is correct — the concrete types are no longer needed when using guards. -
Edge cases: The utility correctly handles
null | undefinedby coalescing tonullbefore passing to the guard. One creative use inLexicalReconciler.test.tsuses a custom inline guard(node): node is TestDecoratorNode => node instanceof TestDecoratorNode— this is valid and shows the utility is flexible. -
www backwards compatibility: This PR only touches test files and a test utility. No production API changes, no export changes, no signature changes. Zero www impact.
-
CI: Core unit tests (node 24.x) and core browser tests pass. CLA signed. Vercel previews deployed. Browser/e2e/integration tests still pending but those are test-only changes that should not regress.
Why this is safe: All changes are test-only. The deprecated generic type parameters still work in production code — this just migrates tests away from them so the deprecation can proceed. No runtime behavior change in any library package.
Description
Migrate the remaining call sites (all in tests) off the deprecated unsafe type parameters that #8661 added to the node traversal methods (getFirstChild(), getParent(), getChildAtIndex(), $getNodeByKey(), etc.). The deprecated overloads themselves are retained for backwards compatibility with consumers.
Add a $assertNodeType test helper that narrows a node via a type guard instead of an unchecked cast, and use it (alongside the existing $is* guards) in place of the deprecated type parameters and a few remaining unsafe
ascasts in the affected tests.Follow-up to #8661
Test plan
Only unit test syntax is affected, behavior should be identical and no runtime code has changed.