process.env in NodeJS with TypeScript: How to PROPERLY Type It (Get Autocomplete + Catch Typos)

preview_player
Показать описание

In this video, I'll show you how you can do that with just a few lines of code.

Specifically, here's what you'll learn in this video:

First, I'll show you how you can extend the global ProcessEnv interface with your custom environment variables in order to get autocomplete working and have each env var with the correct type.

Next, I'll show you how, by enabling a specific tsconfig option, you can get a TypeScript error when an env var is mistyped.

Finally, I'll share with you a recommendation about being more strict with the types of your environment variables.

Hope you'll enjoy this video!

#typescript #nodejs

SUBSCRIBE to learn more tips and tricks that make you a better web developer:
Рекомендации по теме
Комментарии
Автор

Thank you! And to avoid an empty export in the declaration file you can just write like this.

declare namespace NodeJS {
interface ProcessEnv {
PARAM: value;
}
}

narekyazeryan
Автор

Somehow the parent block `declare global {...}` doesn't work for me. So I remove it and everything then works as expected.

sonxuannguyen
Автор

Thank you! But there is still an issue here .. i only get autocomplete when i have the env.d.ts file open in a tab. If i close the tab (close the file) the autocomplete is no longer there.... Any suggestion??

apostolosmavropoulos