Extending instance/static functions on existing prototypes with TypeScript

Since TypeScript 1.4 static extensions can be added easily. The TypeScript team changed the lib.d.ts file to use interfaces for all static type definitions.

The static type definitions are all named like [Type]Constructor: So if you want to add a static function to type Object, then add your definition to ObjectConstructor.

Definition:

interface ObjectConstructor
{
    FooAgain(): void;
}

Implementation:

Object.FooAgain = function(): void
{
    // Oh noes, it's foo again!
}

The problems you are encountering are the following:

Declarations of variables are not open like interfaces, so you can't add properties to an existing declare var ....

Because it is declare var and not declare class you can't extend Object using inheritance.

So you can't get the full experience from TypeScript in this scenario. You can only get the compromise of:

Object['FooAgain'] = function () {
    alert('Again?');
}

Object['FooAgain']();

If you want the full TypeScript experience, I recommend you create a class to house the static methods - they don't operate on an instance anyhow, so there is little harm in doing this:

module Custom {
    export class Object {
        static FooAgain() {
            alert('Again?');
        }
    }
}

Custom.Object.FooAgain();