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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With