I have:
namespace A {
export namespace B {
export class C {constructor(arg:string|A.B.D){}}
}
}
in one file (C.ts
), and:
namespace A {
export namespace B {
export class D {}
}
}
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!
Cheers!
Top comments (8)
I just wanted to point out that TS namespaces are regarded by many to be deprecated. You should use ES6 modules instead.
How do I do that? And how do I continue to keep the C-#-like package system, then?
dev.to/jwp/javascript-namespace-eq...
I'm sorry, but the post didn't make sense?
Is it saying the equivalent of what I want (Java style):
is just:
in a file
folder/subfolder/MyClass.ts
?If not, what am I understanding wrong? And what do I actually want?
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.
Where a function named xyz exists in both filename1 and filename2.
What if I wanna package things together literally? Like in Java?
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.
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?