Extending instance/static functions on existing prototypes with TypeScript

后端 未结 3 1494
你的背包
你的背包 2020-12-14 10:50

I recently asked a question about TypeScript\'s ability to extend existing prototypes in the JavaScript API (here: Extending Object.prototype with TypeScript).

This

相关标签:
3条回答
  • 2020-12-14 11:11

    UPDATE

    It is now possible since TS 1.4. See Stefan's answer above

    LEGACY

    I have this bug raised for a while now: https://typescript.codeplex.com/workitem/917

    One solution that the typescript team could have (without extending the language spec) is have used interfaces for the static members and constructors : http://basarat.github.io/TypeScriptDeepDive/#/modellingstatics

    Basically if lib.d.ts had:

    //Pulled from lib.d.ts
    
    interface Object {
        toString(): string;
        toLocaleString(): string;
        valueOf(): Object;
        hasOwnProperty(v: string): bool;
        isPrototypeOf(v: Object): bool;
        propertyIsEnumerable(v: string): bool;
        [s: string]: any;
    }
    
    interface ObjectStatic {
        new (value?: any): Object;
        (): any;
        (value: any): any;
        prototype: Object;
        ...
    }
    
    declare var Object: ObjectStatic; 
    

    Then you could have easily added members to Object via:

    interface ObjectStatic {
        FooAgain(): void;
    }
    
    Object.FooAgain = function () {
        // TS would be fine with this. 
    }
    

    I just created a feature request for this as well : https://typescript.codeplex.com/workitem/1085

    0 讨论(0)
  • 2020-12-14 11:27

    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();
    
    0 讨论(0)
  • 2020-12-14 11:32

    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!
    }
    
    0 讨论(0)
提交回复
热议问题