@vitruvius-labs/ts-predicate
Version:
TypeScript predicates library
15 lines (14 loc) • 628 B
JavaScript
function getConstructorOf(value) {
// eslint-disable-next-line @ts/no-unsafe-assignment -- Object.getPrototypeOf is badly typed
const PROTOTYPE = Object.getPrototypeOf(value);
if (PROTOTYPE === null) {
throw new Error("The value has no prototype.");
}
// eslint-disable-next-line @ts/no-unnecessary-condition -- Object.create can make constructor-less objects
if (PROTOTYPE.constructor === undefined) {
throw new Error("The value has no constructor.");
}
// @ts-expect-error -- Prototype constructor is badly typed
return PROTOTYPE.constructor;
}
export { getConstructorOf };