Reputation: 692
I had the impression that adding environment variables to environment.d.ts would ensure that they had the proper types.
I have @types/node as a dev-dependency, and have an environment.d.ts containing the following
declare global {
namespace NodeJS {
interface ProcessEnv {
DATABASE_URL: string;
}
}
}
export {};
However, when I attempt to use DATABASE_URL, e.g. in
import PgPromise from "pg-promise";
const db = PgPromise()(process.env.DATABASE_URL || "");
I'm still getting the error
error TS2345: Argument of type 'string | undefined' is not assignable to parameter of type 'string | IConnectionParameters<IClient>'.
Type 'undefined' is not assignable to type 'string | IConnectionParameters<IClient>'.
Is there any way to ensure that DATABASE_URL is of type string, as opposed to string | undefined? Am I missing something?
Upvotes: 5
Views: 12193
Reputation: 692
The error was coming from the fact that I was using ts-node.
I was using nodemon / ts-node, and ts-node uses files / include / exclude to determine what files to monitor. Because I didn't specify these, it wasn't able to detect my files.
I fixed it by adding
{
...,
"ts-node": {
"files": true
},
"include": ["src/**/*.ts", "environment.d.ts"]
}
to my tsconfig.json
Upvotes: 13