Rule Definition
While namespaces sometime have their uses, they add an extra level of indirection when combined with modules. This can quickly become confusing for users, and is usually unnecessary.
The general idea of namespacing is to provide logical grouping of constructs and to prevent name collisions. However since the module file is in itself a logical grouping, and its top-level name is defined by the code that imports it, it is unnecessary to use an additional module layer for exported objects.
Remediation
Favor using modules over namespaces for structuring you code.
Violation Code Sample
//in shapes.ts module
export namespace Shapes {
export class Triangle { /* ... */ }
export class Square { /* ... */ }
}
//in shapeConsumer.ts module
import * as shapes from "./shapes";
let t = new shapes.Shapes.Triangle(); // shapes.Shapes?
Fixed Code Sample
// in shapes.ts module
export class Triangle { /* ... */ }
export class Square { /* ... */ }
// in shapeConsumer.ts module
import * as shapes from "./shapes";
let t = new shapes.Triangle();
Reference
https://www.typescriptlang.org/docs/handbook/namespaces-and-modules.html
https://www.typescriptlang.org/docs/handbook/modules.html
Related Technologies
Technical Criterion
Programming Practices - Structuredness
About CAST Appmarq
CAST Appmarq is by far the biggest repository of data about real IT systems. It's built on thousands of analyzed applications, made of 35 different technologies, by over 300 business organizations across major verticals. It provides IT Leaders with factual key analytics to let them know if their applications are on track.