如何将类型与模块一起导出? [英] How to export typings along with a module?

查看:23
本文介绍了如何将类型与模块一起导出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个模块,为我们团队正在使用的测试套件添加一些功能.这是一个包含主要打字稿文件的项目,它包装了我们项目的其他导出.

I'm writing a module that adds some functionalities to testing suites our team is using. It's a project that has main typescript file, that wraps other exports from our project.

index.d.ts:

index.d.ts:

export * from "./OneModule"
export * from "./AnotherModule"

其中一个模块具有向 Chai.js 断言库添加新方法的功能.这是通过使用它的函数来添加新方法来实现的,例如:

One of the modules has a functionality of adding new methods to Chai.js assertion library. This is achieved by using it's functions for adding new methods, for example:

assertion.addChainableMethod('newMethod', callback, chainCallback)

将增加编写语句的能力,如 expect(something).to.be.newMethod().但是,TypeScript 无法识别这些新方法,因为 @types/chai 中的类型断言显然没有这些功能.

will add ability to write statements like expect(something).to.be.newMethod(). TypeScript though, doesn't recognize those new methods, since the type Assertion in @types/chai obviously doesn't have those functions.

我已经创建了要与 Chai 断言合并的接口定义,如下所示:

I've created interface definition to be merged with the Chai Assertion like this:

declare namespace Chai {
    interface Assertion {
        newMethod(something: string): Assertion;
        newMethod: Assertion;
    }
}

addChainableMethod 添加了新方法,因此它也可以作为 expect(something).to.be.newMethod.equals() 链接,这就是为什么它必须同时定义为属性和方法.

The addChainableMethod adds new method so it can be also chaines as expect(something).to.be.newMethod.equals() that's why it has to be defined both as property and method.

问题是,无论我如何将上述声明添加到 index.d.ts 中,新断言在导入项目中都不可见.我怀疑它以某种方式包装在模块命名空间中(但我可能对此非常错误).我知道我可以做 @types/newAssertions 并在那里定义它们,但这需要用户在 package.json 中包含 2 个项目,并且还需要我们的开发人员处理 2 个项目立刻.如何导出我的 Chai 命名空间扩展和我拥有的所有模块?

The problem is, no matter how I add the above declaration to index.d.ts the new assertions aren't visible in the importing project. I suspect it's somehow wrapped in module namespace(but I might be very wrong about that). I know I can do @types/newAssertions and define them there, but that requires users to include 2 projects in package.json, and also requires our developers to handle 2 projects at once. How can I export both my Chai namespace extension and all the modules I have?

我做过的最好的事情就是这样:

The best I've managed to do is something like that:

declare module 'my-module' {
    function AddNewAssertions(chai: any, utils: any): void;
}
declare namespace Chai {
    interface Assertion { //same as above }
}

但是有了这个,我不能公开我的其他模块,而且只有当我只公开 AddNewAssertions 函数时它才起作用,它添加了这些断言.不过这很管用.

But with this I cannot expose my other modules, and it works only when I'm only exposing AddNewAssertions function, which adds those assertions. This works perfectly though.

推荐答案

我为此编写了一个非常基本的测试应用程序,并在添加以下内容方面取得了一些成功:

I knocked up a very basic test app for this, and I had some success with adding:

declare global {
    namespace Chai {
        interface Assertion {
            newMethod(something: string): Assertion;
            newMethod: Assertion;
        }
    }
}

这使得扩展在您的其他模块中可见,而不仅仅是带有声明的模块.

This makes the extension visible in your other modules, not just the one with the declaration.

这篇关于如何将类型与模块一起导出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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