问题
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