hey TypeScript savvy folks: how can I make it so b...
# help
s
hey TypeScript savvy folks: how can I make it so both the infrastructure/stack code and my Lambda code stop yelling at me for using
process.env.<some var>
? I always have to use the non-null assertion. I’d rather just declare everything so I know I won’t make any mistakes.. e.g.
SENTRY_DSN: string;
r
In the folder you store your types. Create a folder called node, and a file in there called process.d.ts
Copy code
declare namespace NodeJS {
  export interface ProcessEnv {
    SENTRY_RELEASE: string;
    NODE_ENV: 'development' | 'production';
  }
}
s
I know I have to override
NodeJS.ProcessEnv
somehow. I tried this:
Copy code
declare global {
  namespace NodeJS {
    interface ProcessEnv {
      SENTRY_RELEASE: string;
      NODE_ENV: 'development' | 'production';
    }
  }
}
but don’t know where to put that. I tried in the project root as
environment.d.ts
and it doesn’t work
t
What Ross said but you have to make sure your tsconfig is including that folder
s
ok, so ideally I’d want this at project root.
so
src/
and
infra/
can use it
t
You can place it anywhere and then I believe add the path to
compilerOptions.types
Not sure if it's that or the
include
- let me check for you
s
there’s already a tsconfig at project root. but if I put
environment.d.ts
in the root, it doesn’t seem to work.
t
I think what you have to do is put it in
types/environment.d.ts
for example and then
tsconfig.json
should have
compilerOptions.typeRoots = ["./types"]
actually that might break things, try just adding it to
include
s
no such luck 😞
t
let me try to reproduce
Hm it's working for me
Copy code
{
  "extends": "@tsconfig/node14/tsconfig.json",
  "include": [
    "cdk",
    "src",
    "types"
  ],
  "compilerOptions": {
    "module": "esnext",
    "moduleResolution": "node"
  }
}
Copy code
// types/process.d.ts
declare namespace NodeJS {
  export interface ProcessEnv {
    SENTRY_RELEASE: string;
    NODE_ENV: "development" | "production";
  }
}
s
ahh damn it.
sorry, I didn’t look carefully enough. I had
declare global { namespace NodeJS { …
got rid of the global part. now it works!
t
sweet
s
thanks 🙂
also I can just get away with
"include": ["infra", "src", "./*.d.ts"]
so I can keep it at the root level if I want to
r
Cool
s
it would be pretty awesome if it could just read from all the
.env*
files and generate the types internally somehow