Repository navigation
[lexical-utils] Bug Fix: objectKlassEquals throws on undefined and prototype-less objects - #9079
Merged
Merged
Conversation
…ototype-less objects The guard only excluded null, so undefined fell through to Object.getPrototypeOf and threw a TypeError. An object created with Object.create(null) threw too, because getPrototypeOf returns null and .constructor was read off it. The parameter is typed unknown and the existing null check shows these were meant to answer false rather than throw.
nithin42
requested review from
acywatson,
etrepum,
fantactuka,
ivailop7,
potatowagon and
zurfyx
as code owners
August 19, 2026 17:46
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
etrepum
enabled auto-merge
August 19, 2026 18:20
etrepum
approved these changes
Aug 19, 2026
Merged
This branch was successfully deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
objectKlassEqualstakesobject: unknownand returns a type predicate:The guard only excludes
null, so two ordinary values that its ownunknownparameter type permits make it throw instead of returning an answer:objectKlassEquals(undefined, SomeClass)throwsTypeError: Cannot convert undefined or null to objectfromObject.getPrototypeOf.objectKlassEquals(Object.create(null), SomeClass)throws too:Object.getPrototypeOfreturnsnullfor a prototype-less object and.constructoris then read offnull.The existing
!== nullbranch shows the intent was to answerfalsefor nullish input rather than throw; it just covers half of it. A predicate function that throws on part of its declared input domain is awkward for callers, sinceobjectKlassEquals(x, Foo)cannot be used to narrow anunknownwithout a separate nullish check first.Fix
Return
falsefor any nullish input, and for a value whose prototype chain does not yield a constructor, instead of letting either case throw. The behaviour for every value that already worked is unchanged.The repo's
@lexical/internal/no-optional-chainingrule rules out the one-liner, so this is written with explicit early returns.Test plan
Extended the existing
packages/lexical-utils/src/__tests__/unit/LexicalUtilsKlassEqual.test.tsrather than adding a new file, since it already covers this function:On
mainthe new case fails withTypeError: Cannot convert undefined or null to objectatobjectKlassEquals. With this change it passes, and thelexical-utilssuite is green (15 files, 226 tests).I also ran the suites for the packages that call this helper (
lexical-clipboard,lexical-html,lexical-rich-text) to confirm nothing depended on the old behaviour: 31 files, 259 tests, all passing.prettierandeslintare clean.