dotenv icon indicating copy to clipboard operation
dotenv copied to clipboard

DotenvPopulateInput not compatible with NodeJS.ProcessEnv

Open mak3 opened this issue 3 years ago • 4 comments

Seems that populate method just never worked in Typescript. process.env may contain an undefined value in dict, making it unassignable.

import dotenv = require('dotenv')

const parsed = { HELLO: 'world' }
dotenv.populate(process.env, parsed)

Argument of type 'ProcessEnv' is not assignable to parameter of type 'DotenvPopulateInput'. 'string' index signatures are incompatible. Type 'string | undefined' is not assignable to type 'string'. Type 'undefined' is not assignable to type 'string'.ts(2345)

dotenv type: https://github.com/motdotla/dotenv/blob/cf4c56957974efb7238ecaba6f16e0afa895c194/lib/main.d.ts#L107C1-L109C2

node.js built-in type:

interface ProcessEnv extends Dict<string> {}

interface Dict<T> {
    [key: string]: T | undefined;
}

mak3 avatar Aug 12 '23 17:08 mak3

Ok, I learned that I can use "as" to workaround it.

dotenv.populate(process.env as dotenv.DotenvPopulateInput, parsed)

mak3 avatar Aug 12 '23 19:08 mak3

@motdotla should reopen, the type from dotenv is wrong, should be {[key: string]: string | undefined} instead of {[key: string]: string}

jedwards1211 avatar Oct 07 '25 17:10 jedwards1211

forever type adjustments. @jedwards1211 would you mind creating the PR change if you get a chance. would be nice for the community. :)

motdotla avatar Oct 07 '25 17:10 motdotla

Sure. What about the output types? Do you want to leave it up to users' noUncheckedIndexedAccess setting? For the input types we can safely make this change without breaking anything, but for the output types it would be breaking to people who don't use noUncheckedIndexedAccess, unfortunately. I avoid using record types with non-optional properties in lib defs

jedwards1211 avatar Oct 07 '25 20:10 jedwards1211