Home > Net >  How to group several classes without namespaces in typescript
How to group several classes without namespaces in typescript

Time:01-15

From what I understand, typescript seems to discourage namespaces with ES6 modules, however I seem to have a case where I want to group several classes and be sure of what I'm using and where it is coming from. Although I cannot provide my actual code, I am attaching some minimal examples of what I'm trying to achieve, and want to understand the best practice for such a case.

In my code I have files with similar names under various directories, these files however have different use cases such as interacting with an API or interacting with a database.

My folder structure looks something like the following

src/
├── api/
│   └── shape/
│       ├── triangle
│       ├── circle
│       └── square
└── database/
    └── shape/
        ├── tirangle
        ├── circle
        └── square

What I want to achieve

For this question I'm just focusing on the database directory and I want to be able to access my database code in the following manner.

const results = Database.Shape.triangle.methodName();

and also have the ability to import the triangle class for example, directly. This would be the case when the consumer file is not using any other triangle.

I want to do this because I want to be sure that it is the database that I'm accessing since I have classes with similar names for other purposes. I could achieve a 2 level nesting according to have typescript has shown in their documentation, however I'm not quite sure how I would do this with another level of nesting.

export class Triangle {
  /* ... */
}
export class Square {
  /* ... */
}

---

import * as shapes from "./shapes";
let t = new shapes.Triangle();

Just to clarify, I'm in a NodeJS environment.

CodePudding user response:

With your example file structure

src/
├── api/
│   └── shapes/
│       ├── triangle
│       ├── circle
│       └── square

Have an src/api/shape/index.ts file like this:

import Square from "./square";
import Triangle from "./triangle";
import Circle from "./circle";

export default {
    Square, Triangle, Circle
}

And then an src/api/index.ts file like:

import Shapes from "./shapes";

export default {
    Shapes
}

And then let's say you have a src/app.ts file you can do this:

import Api from "./api";

const triangle = Api.Shapes.Triangle.methodName();

==Updated==

Original poster in comments said they wanted to have the option to import Triangle directly from ./api, so here is how you can do that:

src/api/shapes/triangle.ts

export class Triangle {
    static someMethod() {}
}

src/api/shapes/index.ts

export { Square } from "./square";
export { Triangle } from "./triangle";

src/api/index.ts

import * as Shapes from "./shapes";

export * from './shapes'

export default {
    Shapes
}

src/app.ts

import Api, { Triangle } from "./api";

const triangle = Api.Shapes.Triangle.someMethod();
const triangle2 = Triangle.someMethod();

So you'd have either option.

  • Related