Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,14 @@ describe('LexicalUtilsKlassEqual tests', () => {
expect(eventInstance instanceof MyEventShadow).toBeFalsy();
expect(objectKlassEquals(eventInstance, MyEventShadow)).toBeTruthy();
});
it('objectKlassEquals with a nullish or prototype-less value', async () => {
// The parameter is typed `unknown`, and the existing `!== null` guard
// shows nullish input is expected to be answered, not thrown on.
expect(objectKlassEquals(null, MyEvent)).toBe(false);
expect(objectKlassEquals(undefined, MyEvent)).toBe(false);
// Object.getPrototypeOf() returns null for these, so reading
// .constructor off it throws.
expect(objectKlassEquals(Object.create(null), MyEvent)).toBe(false);
});
});
});
11 changes: 8 additions & 3 deletions packages/lexical-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -848,9 +848,14 @@ export function objectKlassEquals<T>(
object: unknown,
objectClass: ObjectKlass<T>,
): object is T {
return object !== null
? Object.getPrototypeOf(object).constructor.name === objectClass.name
: false;
if (object == null) {
return false;
}
const prototype = Object.getPrototypeOf(object);
if (prototype == null || prototype.constructor == null) {
return false;
}
return prototype.constructor.name === objectClass.name;
}

// Clipboard may contain files that we aren't allowed to read. While the event is arguably useless,
Expand Down