Issue
I know for setting .env types, we write this code in .d.ts:
var process: {
env: {
MONGO_DB_URL: string;
}
}
But if I do this in global declaration, like this:
declare global {
var process: {
env: {
MONGO_DB_URL: string;
}
}
module Express {
export interface Request {
decodedToken: DecodedToken;
}
export interface Application {
io: Server;
sessionQIDtoSocketMap: Record<string, string>;
}
}
}
It gives me this error:
var process: globalThis.NodeJS.Process
Subsequent variable declarations must have the same type. Variable 'process' must be of type 'Process', but here has type '{ env: { MONGO_DB_URL: string; }; }'.ts(2403)
globals.d.ts(44, 13): 'process' was also declared here.
If I do something like this:
declare var process: {
env: {
MONGO_DB_URL: string;
};
};
declare global {
module Express {
export interface Request {
decodedToken: DecodedToken;
}
export interface Application {
io: Server;
sessionQIDtoSocketMap: Record<string, string>;
}
}
}
The errors then go away but typescript still doesn't register the types

Solution
I had the same issue a while back. Referencing my .env variables like this solved the problem for me
const MONGODB_URL = process.env["MONGO_DB_URL"];
EDIT
This is what I ended up doing in the end, it's slightly better than my previous answer. In globals.d.ts:
declare namespace NodeJS {
interface ProcessEnv {
MONGO_DB_URL: string
//etc...
}
}
Then just reference .env variables same as before using dot notation. You should get intellisense
When there is declare global, then the code should be:
declare global {
namespace NodeJS {
export interface ProcessEnv {
JWT_SECRET: string;
}
}
}
Answered By - bkw1491
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.