使用 TypeScript 扩展 Object.prototype [英] Extending Object.prototype with TypeScript

查看:47
本文介绍了使用 TypeScript 扩展 Object.prototype的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发一个 TypeScript API,它需要一些附加功能绑定到对象原型 (Object.prototype).

考虑以下代码:

class Foo {}接口对象{GetFoo(): Foo;GetFooAsString(): 字符串;}//这是有问题的...Object.prototype.GetFoo = function() {返回新的 Foo();//注意,这一行只是为了测试...我不希望我的函数只返回一个空的 Foo 实例!}//还行吧.Object.prototype.GetFooAsString = function () {返回 this.GetFoo().toString();}

您可能想直接在 .

一旦您对该代码进行编辑,解析器就会再次遍历代码,并且由于它缓存了旧的接口定义,因此会看到重复的函数定义,然后有效地炸毁了.您对该文件所做的编辑越多,错误语句就会变得越复杂.

I am currently working on a TypeScript API, which requires some additional features binding to the Object prototype (Object.prototype).

Consider the following code:

class Foo {

}

interface Object {
    GetFoo(): Foo;
    GetFooAsString(): string;
}

//This is problematic...
Object.prototype.GetFoo = function() {
    return new Foo();
    // Note, this line is just for testing...I don't want my function to just return a blank instance of Foo!
}

//This is ok.
Object.prototype.GetFooAsString = function () {
    return this.GetFoo().toString();
}

You might want to try this directly at the Playground.

As you can see, I have a class called Foo (not the actual object name I will be using). I have also extended the Object interface to include two new functions. Finally I have implemented the functions against the prototype (these work in pure JavaScript, it's just TypeScript that complains).

Where I have annotated "//this is problematic..." TypeScript highlights this with a red squiggly, and shows the following error:

Cannot convert '() => Foo' to '{ (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; }': Call signatures of types '() => Foo' and '{ (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; (): Foo; }' are incompatible
() => Foo

Either this is just a TypeScript bug (I know it's still in development phase, so a lot of the bugs need ironing out, and I have illustrated some of these on CodePlex already), or, I'm missing something.

Why am I getting this issue?

If it's not a TypeScript bug, how can I fix this?

解决方案

This bug is fixed in TS 0.9.0 alpha as you can see below:

The playground is still running 0.8.3.

This basically happens because methods on some key interfaces ( Object, Number, String ) etc get cached as a performance optimization.

If you run this. The first time it loads you will not see that error. Try It.

As soon as you make an edit to that code, the parser goes through the code again, and since it cached the old interface definition sees a duplicate function definition and then effectively blows up. The more edits you make to that file the more complicated the error statement will get.

这篇关于使用 TypeScript 扩展 Object.prototype的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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