An example of what I want to do in C# looks like this:
namespace SharedFramework
{
public static class Extensions
{
public static void SomeExtension(this object obj)
{
//whatever
}
}
}
An example of what I want to do in C# looks like this:
namespace SharedFramework
{
public static class Extensions
{
public static void SomeExtension(this object obj)
{
//whatever
}
}
}
As Alex Wayne notes, this is generally a bad idea unless you are just using it for polyfills.
But yes, this can be done in Typescript because interfaces are open for extension (docs link). So you just need to extend the Object
interface when you add something to Object.prototype
; or you can extend other interfaces like Array
if you don't want to affect every object.
interface Object {
foo(): void;
}
Object.prototype.foo = () => console.log('bar');
const a: {} = {};
a.foo();