使用TypeScript在现有原型上扩展实例/静态函数 [英] Extending instance/static functions on existing prototypes with TypeScript

查看:144
本文介绍了使用TypeScript在现有原型上扩展实例/静态函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近问了一个关于TypeScript在JavaScript API中扩展现有原型的能力的问题(这里:使用TypeScript扩展Object.prototype )。

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

这结果是一个bug,从那时起就被解决了TypeScript 0.9.0 Alpha(现在已经解决了)包括泛型...伟大: - ))

This turned out to be a bug, which has since been resolved as of TypeScript 0.9.0 Alpha (which now includes generics...GREAT :-))

在TypeScript中,接口是开放式的,所以如果你查看 lib.d.ts ,您将找到一个定义JavaScript的Object API合约的接口。您还应该看到Object的变量声明,它定义了Object的静态函数。

In TypeScript, interfaces are open ended, so if you look in lib.d.ts, you will find an interface which defines the contract for JavaScript's Object API. You should also see a variable declaration for Object, which defines Object's static functions.

为了简单起见,这里它们是:

For the sake of simplicity, here they are:

//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;
}

declare var Object: {
    new (value?: any): Object;
    (): any;
    (value: any): any;
    prototype: Object;
    getPrototypeOf(o: any): any;
    getOwnPropertyDescriptor(o: any, p: string): PropertyDescriptor;
    getOwnPropertyNames(o: any): string[];
    create(o: any, properties?: PropertyDescriptorMap): any;
    defineProperty(o: any, p: string, attributes: PropertyDescriptor): any;
    defineProperties(o: any, properties: PropertyDescriptorMap): any;
    seal(o: any): any;
    freeze(o: any): any;
    preventExtensions(o: any): any;
    isSealed(o: any): bool;
    isFrozen(o: any): bool;
    isExtensible(o: any): bool;
    keys(o: any): string[];
}

所以,我之前的问题是关于扩展Object的接口,如下所示:

So, my previous question was about extending the interface for Object, like so:

interface Object {
    GetFoo(): Foo;
}

Object.prototype.GetFoo = function() {
    return new Foo();
}

如上所述,这与TypeScript 0.9.0一样。

As stated, this works as of TypeScript 0.9.0.

但现在我也希望能够向Object添加静态函数,在纯JavaScript中完全合法。

But now I also want to be able to add static functions to Object, perfectly legal in pure JavaScript.

在JavaScript中我会这样做:

In JavaScript I would do:

Object.FooAgain = function () {
    // Yes, it's foo again!
}

但我似乎无法在TypeScript中完成此任务。

But I can't seem to be able to accomplish this in TypeScript.

首先我试着看看Object的声明是否是开放的(与接口一样):

First I tried to see if Object's declaration was open ended (as with interfaces):

declare var Object: {
    FooAgain(): void;
}

Object.FooAgain = function () {
    // TS doesn't like this, and Object.FooAgain isn't available in intellisense.
}

我也试过了(我在另一篇文章中看到了这一点,并认为值得试试)。

I also tried (I saw this in another article, and thought it was worth a try).

export declare var Object: {
}

但这似乎打破了一切......

but this seems to break everything even more...

不可否认这个问题可能也有已经在TS 0.9.0中修复了(我正在工作,所以我还没有完全尝试过它。)

Admittedly this issue may have also been fixed in TS 0.9.0 (I'm at work so I haven't actually tried it fully).

任何人都可以对此有所了解吗?

Can anyone shed some light on this?

推荐答案

因为可以轻松添加TypeScript 1.4静态扩展。 TypeScript团队更改了 lib.d.ts 文件,以便为所有静态类型定义使用接口。

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.

静态类型定义都命名为 [Type] Constructor :所以如果你想添加一个静态函数来输入 Object ,然后将您的定义添加到 ObjectConstructor

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.

定义:

interface ObjectConstructor
{
    FooAgain(): void;
}

实施:

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

这篇关于使用TypeScript在现有原型上扩展实例/静态函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆