Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript: export all functions in a namespace

Tags:

typescript

Lets say I have a typescript file Utils with a bunch of exported functions:

export function utilOne(){}
export function utilTwo(){}

I added index.d.ts file to this folder where I export * from the Utils file:

export * from './Utils';

In my other classes I'd like to access functions utilOne and utilTwo via utils namespace, like:

utils.utilOne();

I know that I can import it like this:

import * as utils from "./Utils";

However, as I will be using utils a lot, I would like to be able to export utils in a namespace, something like:

export {* as utils} from './Utils';   // this doesn't work

and then use:

import * from "./Utils";

However the export {* as utils} doesn't work. I could put all the functions of Utils to a module "utils" and export it, but I am not sure if this is a good practice. Is there a proper way to do this?

like image 966
zeroin Avatar asked Jul 03 '16 09:07

zeroin


People also ask

How do I export multiple functions from TypeScript?

Use named exports to export multiple functions in TypeScript, e.g. export function A() {} and export function B() {} . The exported functions can be imported by using a named import as import {A, B} from './another-file' . You can have as many named exports as necessary in a single file.

How do I export functions in TypeScript?

Use named exports to export a function in TypeScript, e.g. export function sum() {} . The exported function can be imported by using a named import as import {sum} from './another-file' . You can use as many named exports as necessary in a single file.

How do I export multiple functions?

To export multiple functions in JavaScript, use the export statement and export the functions as an object. Alternatively, you can use the export statement in front of the function definitions. This exports the function in question automatically and you do not need to use the export statement separately.

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.


2 Answers

import * from

No. Global imports are considered bad practice even in languages that support them. (e.g. python Why is "import *" bad?)

JavaScript / TypeScript doesn't support it. After all its pretty useful to see foo.bar and know that bar is coming from foo instead of bar and having no clue where bar is coming from (without cloning and analyzing the whole project). 🌹

like image 157
basarat Avatar answered Sep 18 '22 12:09

basarat


Since TC39/ecma 262 the feature you probably need was merged. So now you can export the module itself like this:

// module.ts

interface Foo{
    bar: string;
}

function A(){

}

function B(){
    
}

export * as MyModule from './module';

And then use it like a module that contains his own context:

import {MyModule} from './module';

type MyType = MyModule.Foo;

MyModule.A();
MyModule.B();

Seems to me it depends on TS version and webpack if you are using it. For me it's webpack 5 and TS v4.1.2 works good. Did not check tree-shaking yet, but looks it should work fine.

like image 43
Sergey Egorov Avatar answered Sep 18 '22 12:09

Sergey Egorov