Using namespaces to avoid importing from each file

239 Views Asked by At

According to this article, you can use TypeScript namespaces to import types/interfaces without including an import:

https://scotch.io/tutorials/3-useful-typescript-tips-for-angular

However if I create a namepsace:

export namespace ST {

  export interface Ifoo {

  }
}

in order to get it recognized, I have to import it like so:

import {ST} from 'suman-types/dts/foo';

export const foo : ST.Ifoo = function () {

};

is there something I am doing wrong? How can I use a namespace to easily import code?

Perhaps this only works with Angular and doesn't really work with CommonJS/Node.js? Not sure why not though.

1

There are 1 best solutions below

4
On

As is mentioned on the page you refer to this is actually not bound to Angular only.

What I can see on the fist glance is that you use export with the namespace itself.

export namespace ST {
      export interface Ifoo { }
}

The example on scotch.io says you‘re supposed to use export only with the nested interfaces. Like this:

namespace ST {
      export interface Ifoo { }
}

This should do the trick. Then no further import should be needed. You‘re supposed to be able to use the namespace throughout the app without any further input statement.