1

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.

Alexander Mills
  • 90,741
  • 139
  • 482
  • 817

1 Answers1

0

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.

  • I wonder if this works if the namespace is included in node_modules instead of in the "include" property of your tsconfig – Alexander Mills Jul 18 '18 at 05:46
  • I have typeRoots set like this, so maybe that will work: ` "typeRoots": [ "./node_modules/@types", "./node_modules/suman-types/dts" ],` – Alexander Mills Jul 18 '18 at 05:47
  • Maybe this link gives you the needed hint as tutorial often presuppose knowledge you don‘t have and leave out important information: https://www.typescriptlang.org/docs/handbook/namespaces.html –  Jul 18 '18 at 06:34
  • Another useful post: https://stackoverflow.com/questions/38582352/module-vs-namespace-import-vs-require-typescript –  Jul 18 '18 at 06:45