Categories
TypeScript Answers

How to export a constant in TypeScript?

Spread the love

Sometimes, we want to export a constant in TypeScript.

In this article, we’ll look at how to export a constant in TypeScript.

How to export a constant in TypeScript?

To export a constant in TypeScript, we can use the export keyword.

For instance, we write

./docs/users/admin.ts

export const adminUser = {
  //...
};

to export the adminUser constant.

Then we can import adminUser in another module by writing

import * as users from "./docs/users/admin";

const { adminUser } = users;

to import the whole module and name the imported module users with

import * as users from "./docs/users/admin";

Then we can reference adminUser with

const { adminUser } = users;

Conclusion

To export a constant in TypeScript, we can use the export keyword.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *