A small polyfill for Object.setPrototypeOf with cross-platform compatibility
82
Implement small helpers that reassign or clone objects with a specified prototype using the compatibility helper provided by the dependency package. Utilities should work with ordinary prototype objects as well as null prototypes, preserving existing own properties and TypeScript typing for callers.
{ role: "guest" } and a prototype object { describe() { return "role:" + this.role; } }, the helper updates the target in place so that Object.getPrototypeOf(result) equals the provided prototype, returns the same reference, and result.describe() produces role:guest. @test{ color: "blue" } and a null prototype, the helper returns the same reference, Object.getPrototypeOf(result) is null, and existing properties remain accessible after the change. @test{ id: 7 } with a prototype { prefix: "id" } yields a new object with a different reference, prototype set to the provided object, inherited prefix, and leaves the original source prototype unchanged. @test{ label: "fallback", status: "pending" } while the target is { label: "explicit" }, applying the prototype keeps the label value as "explicit" and exposes status via the prototype. @test@generates
export function attachPrototype<T extends object>(target: T, proto: object | null): T;
export function cloneWithPrototype<T extends object>(source: T, proto: object | null): T;Provides a cross-runtime prototype assignment helper.
Install with Tessl CLI
npx tessl i tessl/npm-setprototypeof