How to export a variable with TypeScript?

Sometimes, we want to export a variable with TypeScript.

In this article, we’ll look at how to export a variable with TypeScript.

How to export a variable with TypeScript?

To export a variable with TypeScript, we can use the export keyword.

We can export a variable as a default export by writing

const arr = [1, 2, 3];
export default arr;

We use export default to export arr as a default export.

Then we can import it by writing

import arr from "./file1";

We can also export something as a named import.

To do this, we write

const arr = [1, 2, 3];
export { arr };

to export the arr variable.

Then we can import it by writing

import { arr } from "./file1";

Conclusion

To export a variable with TypeScript, we can use the export keyword.