Export constants in typescript
To export constants in typescript, use the export
keyword it will export your constants, variables, or functions.
To use export constants, variables, or functions in typescript, use the import
keyword. using the import
keyword you can easily use exported module.
Today, I’m going to show you How do i export constants in typescript, as mentioned above, I’m going to use the use export
statement to export constants and use them in another file.
Let’s start today’s tutorial how do you export constants in typescript?
In this example, we will do
- export sample constants variable in one file
- import exported constants in another file
- use the imported variable in a file
constants.ts
// 👇️ named export
export const APP_NAME: string = 'Infinitbility';
// 👇️ named export
export const APP_URL: string = 'https://infinitbility.github.io';
In the above typescript example, we exported some string constants variables now we are going to use them in another file.
index.ts
// 👇️ named import
import { APP_NAME, APP_URL } from './constants';
console.log(APP_NAME); // 👉️ "Infinitbility"
console.log(APP_URL); // 👉️ "https://infinitbility.github.io"
TIP: ***Same as import you can export all constants at one line. eg: export { APP_NAME, APP_URL } ***
I hope it helps you, All the best 👍.