我应该如何从包中导出多组类/接口 [英] How should I export multiple groups of classes/interfaces from a package
问题描述
我有一个打字稿包,其中有 2 组类/接口:writeAPI 和 readAPI.两个 API 都有相同命名的类,例如有一个可写的节点"和一个可读的节点".
I have a typescript package where I have 2 groups of classes/interfaces: writeAPI and readAPI. Both API's have identically named classes, for example there is a writable 'Node' and a readable 'Node'.
我想防止在类名中添加组,例如:'WritableNode'、'ReadableNode'.
I would like to prevent to add the group in the name of the class, like so: 'WritableNode', 'ReadableNode'.
是否可以创建一个可以被另一个包使用的包,如下所示:1)
Is it possible to create a package that can be consumed by another package as follows: 1)
import * as myAPIs from "myAPIs"
const readableNode = new myAPIs.readable.Node()
const writableNode = new myAPIs.writable.Node()
或者(不太受欢迎):2)
or alternatively (less preferred): 2)
import * as myReadableAPI from "myAPIs/readable"
import * as myWritableAPI from "myAPIs/writable"
const readableNode = new myReadableAPIs.Node()
const writableNode = new myWritableAPIs.Node()
这似乎有效(特别适用于我的包裹):
this seems to work (for my package specifically):
import * as myReadableAPI from "myAPIs/dist/src/readable
我觉得它又丑又长,我想去掉多余的目录.我更喜欢上面的第一个选项.
I find it ugly and long, I would like to get rid of the extra directories. And I prefer the first option above.
推荐答案
在(最好)两个文件 /readable.ts
中声明你的 apis &/writeable.ts
并将它们重新导出到您想要的命名空间下的 /apiEntry.ts
文件中.
Declaring your apis in (preferably) two files /readable.ts
& /writeable.ts
and rexport them in an /apiEntry.ts
file under the namespace you want.
import * as _writable from "./writeable"
import * as _readable from "./readable"
export const writeable = _writable
export const readable = _readable
是的,这看起来有点脏,将您的模块重新声明为常量,但是语法 export * as namespace from "./module"
不起作用.无论如何,我相信像 terser 这样的 minifier 可以解决这个问题.
Yes this looks a bit dirty, to redeclare your modules as constants, but the syntax export * as namespace from "./module"
does not work. Anyway I believe minifier like terser can mangle this.
这篇关于我应该如何从包中导出多组类/接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!