According to Alex Wayne, engaging in such practices is usually not recommended, unless it is solely for polyfill purposes.
It is indeed possible to achieve this in Typescript due to the extensibility of interfaces (see documentation). By extending the Object
interface, you can introduce new functionalities to Object.prototype
; alternatively, you can extend specific interfaces like Array
without impacting every object.
interface Object {
foo(): void;
}
Object.prototype.foo = () => console.log('bar');
const a: {} = {};
a.foo();
Access the example on the Typescript Playground via this link to the playground.