gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-30 20:41:48 25 4
gpt4 key购买 nike

我的问题与这个问题几乎相同: 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 将事物导出为命名空间的一部分,而不是单独导出它们?

我有这个正在接近:

enter image description here

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

enter image description here

但如您所见,我的一些声明是接口(interface),而不是类,在那种情况下,我收到错误消息“仅引用类型,但在此处用作值”。

最佳答案

例如,创建一个文件名 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 = {...}

关于typescript - 使用 TypeScript 将导入导出为命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47500855/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com