How should I export multiple groups of classes/interfaces from a package

☆樱花仙子☆ 提交于 2020-06-17 13:23:06

问题


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'.

I would like to prevent to add the group in the name of the class, like so: 'WritableNode', 'ReadableNode'.

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()

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()

edit:

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.


回答1:


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

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.

Sandbox



来源:https://stackoverflow.com/questions/61863910/how-should-i-export-multiple-groups-of-classes-interfaces-from-a-package

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!