使用 TypeScript 将导入导出为命名空间

exporting imports as a namespace with TypeScript

我的问题与这个问题几乎相同: https://github.com/Microsoft/TypeScript/issues/4529

假设我有这个:

//exported imports
export {ISumanOpts, IGlobalSumanObj} from 'suman-types/dts/global';
export {ITestCaseParam} from 'suman-types/dts/test-suite';
export {IHookParam} from 'suman-types/dts/test-suite';
export {IDescribeFn} from 'suman-types/dts/describe';
export {ItFn, ITestDataObj} from 'suman-types/dts/it';
export {IBeforeFn} from 'suman-types/dts/before';
export {IBeforeEachFn} from 'suman-types/dts/before-each';
export {IAfterFn} from 'suman-types/dts/after';
export {IAfterEachFn} from 'suman-types/dts/after-each';
export {DefineObjectContext as IDefObjCtx} from "./test-suite-helpers/define-options-classes";
export {DefineObjectTestCase as IDefObjTestCase} from "./test-suite-helpers/define-options-classes";
export {DefineObjectAllHook as IDefObjAllHook} from "./test-suite-helpers/define-options-classes";
export {DefineObjectEachHook as IDefObjEachHook} from "./test-suite-helpers/define-options-classes";


export namespace s {

  // ! I want to move all of the above exported items into a namespace here

}

有没有办法使用 namespacemodule 将事物导出为命名空间的一部分,而不是单独导出它们?

我有这个正在接近:

所以我尝试将它们更改为导入,然后像这样将它们放在常量上:

但是如您所见,我的一些声明是接口,而不是 类,在那种情况下,我收到了错误消息 "only refers to a type, but is being used as a value here"。

创建一个文件名 s.ts,例如,您要在其中导出命名空间的所有内容:

export {ISumanOpts, IGlobalSumanObj} from 'suman-types/dts/global';
export {ITestCaseParam} from 'suman-types/dts/test-suite';
export {IHookParam} from 'suman-types/dts/test-suite';
export {IDescribeFn} from 'suman-types/dts/describe';
export {ItFn, ITestDataObj} from 'suman-types/dts/it';
export {IBeforeFn} from 'suman-types/dts/before';
export {IBeforeEachFn} from 'suman-types/dts/before-each';
export {IAfterFn} from 'suman-types/dts/after';
export {IAfterEachFn} from 'suman-types/dts/after-each';
export {DefineObjectContext as IDefObjCtx} from "./test-suite-helpers/define-options-classes";
export {DefineObjectTestCase as IDefObjTestCase} from "./test-suite-helpers/define-options-classes";
export {DefineObjectAllHook as IDefObjAllHook} from "./test-suite-helpers/define-options-classes";
export {DefineObjectEachHook as IDefObjEachHook} from "./test-suite-helpers/define-options-classes";

然后在你的模块中你可以这样做:

import * as s from './s'

export {s}

它将在名为 s 的命名空间中导出类型和值。然后您可以使用 :

导入它们
import {s} from 'your-module'

const anObject: s.ISumanOpts = {...}

我做的是这样的:

import * as Block from './block'
import * as FullNode from './full-node'
import * as ListOnChain from './list-on-chain'
import * as HashTools from './hash-tools'
import * as KeyValueStorage from './key-value-storage'
import * as SequenceStorage from './sequence-storage'
import * as SmartContract from './smart-contract'
import * as NodeBrowser from './node-browser'
import * as NetworkApi from './network-api'
import * as NetworkClientBrowserImpl from './network-client-browser-impl'
import * as NodeApi from './node-api'
import * as NodeImpl from './node-impl'
import * as NodeTransfer from './node-transfer'
import * as NodeNetworkClient from './node-network-client'
import * as WebsocketConnector from './websocket-connector'

export {
    Block,
    FullNode,
    ListOnChain,
    HashTools,
    KeyValueStorage,
    SequenceStorage,
    SmartContract,
    NodeBrowser,
    NetworkApi,
    NetworkClientBrowserImpl,
    NodeApi,
    NodeImpl,
    NodeTransfer,
    NodeNetworkClient,
    WebsocketConnector
}

然后在另一个文件中,我可以这样导入,所有都会留在自己的space :

import * as AllComponents from 'blockchain-js-core'

如果你想一一导入,你可以这样做:

import {
    Block,
    FullNode,
    ListOnChain,
    HashTools,
    KeyValueStorage,
    SequenceStorage,
    SmartContract,
    NodeBrowser,
    NetworkApi,
    NetworkClientBrowserImpl,
    NodeApi,
    NodeImpl,
    NodeTransfer,
    NodeNetworkClient,
    WebsocketConnector
} from 'blockchain-js-core'

可在此处找到示例:https://github.com/ltearno/blockchain-js/blob/master/blockchain-js-core/src/index.ts

这是我的解决方案...

import { JobEntity as _JobEntity } from '../types';

export namespace JobList {
  export type JobEntity = _JobEntity;
  export interface Props {
    ---------
  }
}