Extending instance/static functions on existing prototypes with TypeScript

后端 未结 3 1516
你的背包
你的背包 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: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!
    }
    

提交回复
热议问题