How to import a namespace in TypeScript

Sometimes you want to export a custom package (namespace with several classes) and use it with a single import command.

Export a namespace

Let’s say you have a folder called “Logging” with an interface and a class in it. The namespace and the classname must both be exported.

Then you need to setup a file called “index.d.ts” which has all the exports in it.

Importing the classes from the namespace

To import the namespace or classes, you would use a single command like this:

import * as Logging from "./Logging";

To see a working project, please have a look into my GitHub repo with an example file structure, namespace and more classes.

Leave a Reply

Your email address will not be published. Required fields are marked *