使用TypeScript将导入导出为名称空间 [英] exporting imports as a namespace with TypeScript

查看:498
本文介绍了使用TypeScript将导入导出为名称空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

My question is pretty much the same as this one: 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将事物导出为命名空间的一部分,而不是单独导出它们?

Is there a way to use namespace or module to export things as a part of a namespace instead of individually exporting them?

我有这个正在接近:

所以我尝试将它们更改为import,然后将它们放在这样的const上:

So I tried changing them to imports and then putting them on a const like so:

但是正如您所看到的,我的一些声明是接口,而不是类,在那种情况下,我收到错误消息仅引用类型,但在此处用作值".

But as you can see, some of my declarations are interfaces, not classes, and in that case looks like I get the error message "only refers to a type, but is being used as a value here".

推荐答案

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

Create a file name s.ts for example, where you want to export everything for your namespace :

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";

然后在您的模块中,您可以执行以下操作:

Then in your module you can just do :

import * as s from './s'

export {s}

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

It will export both types and values in a namespace called s. You can then import them using :

import {s} from 'your-module'

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

这篇关于使用TypeScript将导入导出为名称空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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