DEV Community

Calin Baenen
Calin Baenen

Posted on

Why do I keep getting `Error: Namespace A.B does not have exported member D.`?

I have:

namespace A {
  export namespace B {
    export class C {constructor(arg:string|A.B.D){}}
  }
}
Enter fullscreen mode Exit fullscreen mode

in one file (C.ts), and:

namespace A {
  export namespace B {
    export class D {}
  }
}
Enter fullscreen mode Exit fullscreen mode

in another (D.ts), and I keep getting an error (in the first file): Namespace A.B does not have exported member D.

Why is this happening? And how can I fix it?

Thanks!
Cheers!

Top comments (8)

Collapse
 
ducaale profile image
Mohamed Dahir

I just wanted to point out that TS namespaces are regarded by many to be deprecated. You should use ES6 modules instead.

Collapse
 
baenencalin profile image
Calin Baenen

How do I do that? And how do I continue to keep the C-#-like package system, then?

Collapse
 
jwp profile image
John Peters
Thread Thread
 
baenencalin profile image
Calin Baenen • Edited

I'm sorry, but the post didn't make sense?
Is it saying the equivalent of what I want (Java style):

package folder;
public class MyClass {}
Enter fullscreen mode Exit fullscreen mode

is just:

export class MyClass {}
Enter fullscreen mode Exit fullscreen mode

in a file folder/subfolder/MyClass.ts?

If not, what am I understanding wrong? And what do I actually want?

Thread Thread
 
jwp profile image
John Peters

The article indicates that in the JavaScript module system, the namespace equivalent is the filename. Want an import? Just use the proper filename.

As far as namespace collisions, which are bound to happen this syntax fixes that problem.

import {xyz as abc} from 'filename1';
import {xyz as def} from 'filename2';

Enter fullscreen mode Exit fullscreen mode

Where a function named xyz exists in both filename1 and filename2.

Thread Thread
 
baenencalin profile image
Calin Baenen

What if I wanna package things together literally? Like in Java?

Thread Thread
 
jwp profile image
John Peters

I think what you want is the concept of an NPM Package. Each package can contain one or more (related) files which are javascript modules. If you need to separate concerns then that is done by creating a different package name. Angular is a good example of this, all it's packages start with the name @angular but then they have different parts like @angular/cli @angular/forms @anuglar/common/http

This is how they contain different things in a similar manner as how namespaces delimit function.

Thread Thread
 
baenencalin profile image
Calin Baenen

I don't want an NPM package, because that means it can only really be applicable to Node (maybe I'm wrong), what I want is the language equivalent.
Also, with a NPM package, how would subpackages work?