Issue
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?
Solution
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). 🌹
Answered By – basarat
Answer Checked By – Senaida (BugsFixing Volunteer)