Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extend @types/node with custom project environment variable type decelerations

I have a project that uses dotenv for environment variables, and I want to add custom project-specific typings for the variables the project uses, for autocompletion, etc.

How can I do this without overriding all the stuff that @types/node already does?

like image 318
James Jensen Avatar asked Dec 08 '25 09:12

James Jensen


1 Answers

You can augment the ProcessEnv interface which is the type of process.env:

// node.augmentations.d.ts
declare namespace NodeJS {
    interface ProcessEnv {
        db: string
        port: string
    }
}

//otherfile.ts
process.env.port //suggested
process.env.db // suggested
process.env.dbb // still ok 😞

This approach will give you auto-completion, although it will not prevent you from accessing other members on env since ProcessEnv defines an index signature.

like image 126
Titian Cernicova-Dragomir Avatar answered Dec 11 '25 00:12

Titian Cernicova-Dragomir