I am working with the below code-block, I built it a couple of months ago in JavaScript, but las week I decided to start learning TypeScript. I cant seem to find how to properly defined the data types. Does any one have any hints or resources that can aid me to solve this issue?
this is the exact error message:
src/utils/pool.ts:5:5 - error TS2322: Type '"" | { rejectUnauthorized: false; } | undefined' is not assignable to type 'boolean | ConnectionOptions | undefined'. Type '""' is not assignable to type 'boolean | ConnectionOptions | undefined'.
5 ssl: process.env.PGSSLMODE && { rejectUnauthorized: false },
Thanks so much!
import { Pool, PoolConfig } from 'pg';
export const pool = new Pool({
connectionString: process.env.DATABASE_URL,
ssl: process.env.PGSSLMODE && { rejectUnauthorized: false },
})
pool.on('connect', ()=> console.log('Postgres connected'))
CodePudding user response:
If you interpret the log output it clearly states there is an issue is with the type which is expected and the type which you have declared.
The Expected type is, boolean or ConnectionOptions or undefined.
The Given type is, "" or { rejectUnauthorized: false; } or undefined
You need to assign a variable of the appropriate type or update the type of currently assigned variable.
CodePudding user response:
From what the error indicates seems like you are using a string where a type 'boolean | ConnectionOptions | undefined' is expected Try this syntax instead
import { Pool, PoolConfig } from 'pg';
export const pool = new Pool({
connectionString: process.env.DATABASE_URL,
...( process.env.PGSSLMODE ? {ssl: { rejectUnauthorized: false }} : {}),
})
pool.on('connect', ()=> console.log('Postgres connected'))