Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 757
Alexander Mills Avatar asked Nov 26 '17 20:11

Alexander Mills


People also ask

How do you define a namespace in TypeScript?

We can create a namespace by using the namespace keyword followed by the namespace_name. All the interfaces, classes, functions, and variables can be defined in the curly braces{} by using the export keyword. The export keyword makes each component accessible to outside the namespaces.

What is export as namespace?

The export as namespace form creates a global variable so it can be used without importing, but you may still import it with the import { name } from "some-library" form of import.

Should you use TypeScript namespaces?

Don't use Custom TypeScript Modules and Namespaces Since we have ES6 modules as a standard in JavaScript, we don't need custom TypeScript modules and namespaces to organize our code. Instead, we should use standard JavaScript modules with import and export instead.

What is the difference between namespace and module in TypeScript?

1. A module is a way which is used to organize the code in separate files and can execute in their local scope, not in the global scope. A namespace is a way which is used for logical grouping of functionalities with local scoping. 2.


2 Answers

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 = {...}
like image 121
Fathy Avatar answered Oct 14 '22 20:10

Fathy


What I do is this :

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
}

Then in another file, I can import like this, and all will stay in their own space :

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

If you want to import them one by one, you can do :

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

An example can be found here : https://github.com/ltearno/blockchain-js/blob/master/blockchain-js-core/src/index.ts

like image 32
Arnaud Tournier Avatar answered Oct 14 '22 21:10

Arnaud Tournier