typescript 向打字稿中的现有类添加方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42999765/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 04:22:54  来源:igfitidea点击:

Add a method to an existing class in typescript?

javascripttypescript

提问by ackuser

I am working in an angular 2 cli project in which I have to create a definition of a plugin, because it doesn't exists its typed. This plugin depends of a main library that has already its own typed and it works.

我正在一个 angular 2 cli 项目中工作,我必须在其中创建插件的定义,因为它不存在其类型。这个插件依赖于一个已经有自己的类型并且可以工作的主库。

Anyway, I have two files the main one with

无论如何,我有两个文件,主要的一个

LIBRARY TYPES FILE A

库类型文件 A

export class A extends B {
    constructor(...);
    methodX(): void;
}

And I would need to add a new method for my plugin so my class would be like

我需要为我的插件添加一个新方法,这样我的班级就会像

export class A extends B {
        constructor(...);
        methodX(): void;
        methodY(): void;
    }

The point is that I need to add it in a separate file. The problem is adding a method to an existent class without creating a new one

关键是我需要将它添加到一个单独的文件中。问题是向现有类添加方法而不创建新方法

If I put

如果我把

PLUGIN TYPES FILE B

插件类型文件 B

export class A extends B {
    constructor(...);
    methodX(): void;
}

or

或者

PLUGIN TYPES FILE B

插件类型文件 B

export class A extends B {
        constructor(...);
        methodX(): void;
        methodY(): void;
}

It doesn't work, does anyone how can I achieve overwriting a class or extending it with a new method that?

它不起作用,有没有人如何实现覆盖类或使用新方法扩展它?

Thanks

谢谢

回答by toskv

You could do it by making an interface with the new method and modifying the prototype.

您可以通过使用新方法创建接口并修改原型来实现。

Something like this:

像这样的东西:

class B { }

class A extends B {
    constructor() {
        super();
    }
    methodX(): void { };
    methodY(): void { };
}


interface B {
    newMethod(): void;
}

B.prototype.newMethod = function () { console.log('a') };

This allows you do have proper typing when doing.

这允许你在做的时候有正确的打字。

new A().newMethod();

I made a playground example here.

我在这里做了一个游乐场的例子。

回答by Bünyamin Sar?gül

You can do it directly as A.prototype.functionName = function(){...}

你可以直接做 A.prototype.functionName = function(){...}

Here is a plunker: http://plnkr.co/edit/6KrhTCLTHw9wjMTSI7NH?p=preview

这是一个plunker:http://plnkr.co/edit/6KrhTCLTHw9wjMTSI7NH?p=preview

回答by Serban Stokker

The "Declaration Merging > Module Augmentation" section from the TypeScript docs seems to offer the solution:

TypeScript 文档中的“声明合并 > 模块增强”部分似乎提供了解决方案:

https://www.typescriptlang.org/docs/handbook/declaration-merging.html#module-augmentation

https://www.typescriptlang.org/docs/handbook/declaration-merging.html#module-augmentation

In your case, if class Ais exported from file1.ts, and you want to add methodY()to that class within a different module file2.ts, then try this:

在您的情况下,如果class A从 导出file1.ts,并且您想methodY()在不同的模块中添加到该类file2.ts,请尝试以下操作:

//// file1.ts
export class A extends B {
    constructor(...);
    methodX(): void;
}

//// file2.ts
import { A } from "./file1";
declare module "./file1" {
    interface A {
        methodY(): void;
    }
}
A.prototype.methodY = function() {}