Warning: session_start(): open(/tmp/sess_c9615b9b1591e901a4857e292a27af40, O_RDWR) failed: Disk quota exceeded (122) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: session_start(): Failed to read session data: files (path: /tmp) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_mkdir() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 562

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230
How To Define Types For Process.env In TypeScript - LearnShareIT

How To Define Types For Process.env In TypeScript

Define types for process.env in TypeScript

If you don’t know how to define types for process.env in TypeScript, this tutorial will help you. Check out the information now.

Define types for process.env in TypeScript

Process.env is often used to store variables in each working environment.

The fact that you save in the environment variable, all JavaScript files can get its value. You just need to edit the environment variables instead of having to open each file to edit. This will save your time in developing the system as well as fixing bugs if there are errors.

Here I will show you how to define types for process.env in TypeScript:

First, you need to declare the types in the global namespace in the file ‘environment.d.ts‘.

Open the ‘src’ folder, and create a subfolder named ‘types’. In ‘types’, create the ‘environment.d.ts‘ file. Declare properties for the ProcessEnv interface. For example, here is your environment.d.ts:

export {}

declare global {
  namespace NodeJS {
    interface ProcessEnv {
      BACKEND_URL: string;
      DB_PORT: number;
      DB_NAME: string;
      ENV: 'production' | 'development';
    }
  }

}

In this example, we declare four properties: BACKEND_URL, DB_PORT, DB_NAME, ENV, and their type. This is just a concrete example. These properties may be different in your project.

Finally, the ‘export {}‘ statement is used to export it as an external module.

After successfully declaring the properties for interface ProcessEnv, you can now try to access them. The system will now automatically complete them for you. If you do not get autocompletion, open the ‘tsconfig.json‘ file and add a path to directory ‘types‘, then the ‘tsconfig.json‘ should look like this:

{
  "compilerOptions": {
    …
    "typeRoots": ["./node_modules/@types", "./src/types"]
    …
  }
}

Sometimes, it can be helpful if you restart the IDE before continuing.

We must install the ‘dotenv’ package before adding values ​​to the specific properties of the process.env object. Otherwise, you will get an undefined value when accessing. Open the terminal and run these commands::

npm install dotenv
npm install -D @types/node

Create a new file called ‘.env‘ in your root directory. Here, you can set values to properties:

BACKEND_URL: "http://localhost:8080";
DB_PORT: 3306;
DB_NAME: "Employees_Database";
ENV: "development";

Now, try accessing the properties of the process.env object and see what we got:

In index.ts:

import 'dotenv/config';

let backend_url = process.env.BACKEND_URL
let database_port = process.env.DB_PORT
let database_name = process.env.DB_NAME
let environment = process.env.ENV

console.log(backend_url);
console.log(database_port);
console.log(database_name);
console.log(environment);

Output:

"http://localhost:8080"
3306
Employees_Database
development

As a final point, you have successfully accessed the properties of the process.env object. However, remember to import the “dotenv” package before you start.

Summary

You have learned how to define types for process.env in TypeScript. I hope the information in this article will help you in your project. If you have any questions, please comment below. We will answer as possible. Thank you for reading!

Maybe you are interested:

Leave a Reply

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