Skip to content
Advertisement

TypeScript: undefined is not assignable to type ‘boolean | ConnectionOptions | undefined

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'))

Advertisement

Answer

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'))
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement