Lately in TypeScript discussions, there seems to be a negative viewpoint on namespace BAD. However, I see value in organizing related declarations within a single namespace, similar to a library, to avoid excessive import statements.
I have come across the feature of using as in imports for namespacing, like this:
import * as NS from '../some/module'
But I find this approach cumbersome and potentially messy if any refactoring is needed for NS (as shown above).
So, I have devised a strategy as outlined below. But I can't shake off the feeling that it might be considered hackish, with potential unforeseen consequences that I am not aware of.
Assuming the following folder structure
.
└── doc
├── doc.ts
├── impl.ts
├── exports.ts
└── index.ts
doc.ts
export enum Type {
A = "A",
B = "B"
}
export interface IDoc {
type: Type;
}
impl.ts
import { IDoc, Type } from "./doc";
export class Impl implements IDoc {
constructor(public type: Type) {
}
}
exports.ts exports everything
export * from "./impl";
export * from "./doc";
index.ts then re-exports 'exports' as Doc:
import * as Doc from "./exports";
export { Doc };
This allows for a consistent single import, for example:
import { Doc } from "../core/doc";
export class SomeClass {
doc: Doc.IDoc;
constructor(type: Doc.Type) {
this.doc = new Doc.Impl(type);
}
}
Are there any downsides to this strategy? Is there a best practice or pattern that I may be overlooking?