Skip to content

[lexical-utils] Bug Fix: objectKlassEquals throws on undefined and prototype-less objects - #9079

Merged
etrepum merged 1 commit into
meta:mainfrom
nithin42:fix-object-klass-equals-nullish
Aug 19, 2026
Merged

etrepum merged 1 commit into
meta:mainfrom
nithin42:fix-object-klass-equals-nullish

Conversation

@nithin42

Copy link
Copy Markdown
Contributor

Description

objectKlassEquals takes object: unknown and returns a type predicate:

export function objectKlassEquals<T>(object: unknown, objectClass: ObjectKlass<T>): object is T {
  return object !== null
    ? Object.getPrototypeOf(object).constructor.name === objectClass.name
    : false;
}

The guard only excludes null, so two ordinary values that its own unknown parameter type permits make it throw instead of returning an answer:

  • objectKlassEquals(undefined, SomeClass) throws TypeError: Cannot convert undefined or null to object from Object.getPrototypeOf.
  • objectKlassEquals(Object.create(null), SomeClass) throws too: Object.getPrototypeOf returns null for a prototype-less object and .constructor is then read off null.

The existing !== null branch shows the intent was to answer false for 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, since objectKlassEquals(x, Foo) cannot be used to narrow an unknown without a separate nullish check first.

Fix

Return false for 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-chaining rule 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.ts rather than adding a new file, since it already covers this function:

npx vitest --project unit --no-watch packages/lexical-utils

On main the new case fails with TypeError: Cannot convert undefined or null to object at objectKlassEquals. With this change it passes, and the lexical-utils suite 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. prettier and eslint are clean.

…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.
@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 Aug 19, 2026
@vercel

vercel Bot commented Aug 19, 2026 •

Copy link
Copy Markdown

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

Project Deployment Actions Updated (UTC)
lexical Ready Ready Preview Aug 19, 2026 5:48pm
lexical-playground Ready Ready Preview Aug 19, 2026 5:48pm

Request Review

@etrepum
etrepum added this pull request to the merge queue Aug 19, 2026
Merged via the queue into meta:main with commit f8492d2 Aug 19, 2026
44 checks passed
@etrepum etrepum mentioned this pull request Aug 31, 2026

This branch was successfully deployed

2 active deployments
Preview – lexical — c0b36c19 Deployed Aug 19, 2026 by vercel[bot]
Preview – lexical-playground — c0b36c19 Deployed Aug 19, 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. extended-tests Run extended e2e tests on a PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants