Search code examples
typescripttypescript2.0suman

exporting imports as a namespace with TypeScript


My question is pretty much the same as this one: https://github.com/Microsoft/TypeScript/issues/4529

Say I have this:

//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

}

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

I have this which is getting close:

enter image description here

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

enter image description here

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


Solution

  • 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}
    

    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 = {...}