SolvedDefinitelyTyped @types/nodemailer 6.2.0 & Typescript 3.5.1 Failure

Compile failure:

error TS2345: Argument of type '{ host: string; port: number; auth: { user: string; pass: string; ssl: boolean; }; }' is not assignable to parameter of type 'Transport | TransportOptions'.
Object literal may only specify known properties, and 'host' does not exist in type 'Transport | TransportOptions'.

When calling:

createTransport({
    host: config.smtpHost,
    port: config.smtpPort,
    auth: {
      user: config.smtpUsername,
      pass: config.smtpPassword,
      ssl: true
    }
  });

Using Typescript 3.4.1 works, but upgrading to 3.5.1 breaks the typings.

FYI @dex4er

15 Answers

✔️Accepted Answer

@ruipaulo ah, in your example port is a string and this is incorrect because it have to be number.

Replace it with:

port: Number(process.env.EMAIL_PORT) || 0

and it should be ok.

Other Answers:

I'm a little late to the party, but I found this worked:

createTransport({
    host: config.smtpHost,
    port: config.smtpPort,
    auth: {
      user: config.smtpUsername,
      pass: config.smtpPassword
    }
  } as SMTPTransport.Options);

The all-important bit was casting it as a SMTPTransport.Options type, as by default it was expecting the configuration to be a Transport | TransportOptions type

I'm getting this error for everything

  const transporter = nodemailer.createTransport({
    service: 'gmail'', //<--- error there
    auth: {
      xoauth2: xoauth2.createXOAuth2Generator({
        user: process.env.SMPT_USER, 
      })
    }
  })

I have tried to mimic above with

service: String('gmail') || '',
// also
 service: String(process.env.SMTP_SERVICE)

it seems like for me that whenever I add the auth: xoaouth2 that's when it all starts to break

More Issues: