简体   繁体   English

在打字稿中使用d.ts扩展外部模块

[英]Extending external module with d.ts in typescript

I am building a nodejs application using typescript. 我正在使用Typescript构建Node.js应用程序。 I'm using several external libraries like express.js. 我正在使用几个外部库,例如express.js。 As many, this library as well designed to be extendable. 与许多库一样,该库还具有很好的可扩展性。

I'd like to extend it by adding a custom method. 我想通过添加自定义方法来扩展它。 What is the typescript best practice to do so? 打字的最佳实践是什么?

I wanted to inherit a class from it, but its d.ts doesn't define any classes (obviously). 我想从中继承一个类,但是(显然)它的d.ts没有定义任何类。 I can extend the interface: 我可以扩展接口:

declare module Express {
    export interface Application {
        foo();
    }
}

but I cannot figure out how to actually implement it. 但我不知道如何实际实施。

Any help would be appreciated. 任何帮助,将不胜感激。

You shouldn't add members to Application directly unless you want to make a reusable library that other people should use. 除非您要创建其他人应该使用的可重用库, 否则不应将成员直接添加到Application If that is indeed the case you will need function interception ( http://basarat.github.io/this-and-prototype/#/apply ). 如果确实如此,则需要功能拦截( http://basarat.github.io/this-and-prototype/#/apply )。

I recommend just making a utility function. 我建议只做一个实用程序功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM