logo

Constatic

Discord Bot BaseConventions

Imports

Development standards for this project

Import Shortcuts

In this project, you will find the Node.js feature called import alias.
You can import anything using alias in package.json.

With this, you can export everything from an index file of these alias and easily import it anywhere in your code. See the example below:

Let's export this simple function from the functions folder:

src/functions/math.ts
export function sum(a: number, b: number){
  return a + b;
}
Export it in the index file of the functions folder, as defined in the package.json and tsconfig.json files.
src/functions/index.ts
export * from "./math.js"

Note that since this project uses the module type, we need to add the .js extension at the end. With this, we can easily import this function in any file, regardless of its depth in our code.

src/discord/commands/admin/context/test.ts
import { sum } from "#functions"

Without this, it would be necessary to use a relative path. See how it would look:

src/discord/commands/admin/context/test.ts
import { sum } from "../../../../../functions/math.js"

In summary, import shortcuts make it easier to import anything and make the code more readable and organized.


On this page