sendgrid-nodejs icon indicating copy to clipboard operation
sendgrid-nodejs copied to clipboard

Sendgrid with TypeScript > 4.4

Open jvineveld opened this issue 3 years ago • 5 comments

Issue Summary

Updating typescript broke sendgrid because of the class instance export When trying to set API key by calling 'setApiKey', it will fail because 'this' is undefined.

The issue is the result of this change in TypeScript: https://devblogs.microsoft.com/typescript/announcing-typescript-4-4/#more-compliant-indirect-calls-for-imported-functions

Steps to Reproduce

Install sendgrid on an typescript > 4.4 environment try to use sendgrid to send a mail

Technical details:

  • sendgrid-nodejs version: 7.2.6
  • node version: 14.17.3

jvineveld avatar Feb 28 '22 15:02 jvineveld

This issue has been added to our internal backlog to be prioritized. Pull requests and +1s on the issue summary will help it move up the backlog.

childish-sambino avatar Mar 09 '22 15:03 childish-sambino

Any update on this? Or potential mitigation? This is essentially blocking upgrading to TypeScript >=4.4 👀

anttispitkanen avatar Mar 17 '22 10:03 anttispitkanen

Seems like I was able to work around this by importing and instantiating a MailService class and using the methods via that. So instead of

import { send, setApiKey  } from '@sendgrid/mail'

setApiKey(environment.sendgrid.apiKey)

// ...and usage...
await send(/* something *)

doing it like

import { MailDataRequired, MailService } from '@sendgrid/mail'

const SendGrid = new MailService()

SendGrid.setApiKey(environment.sendgrid.apiKey)

// ...and usage...
await SendGrid.send(/* something */)

but please feel free to let me know if there's something wrong with this approach. Seems to work for TypeScript versions older and newer than 4.4.x.

anttispitkanen avatar Mar 17 '22 17:03 anttispitkanen

After some testing it indeed seems like my solution above works, so is this more a question of updating the documentation to reflect that as the correct way of doing things? 👀

anttispitkanen avatar Mar 24 '22 14:03 anttispitkanen

Just got bit by this and it seems a pretty breaking issue for it to simply be backlogged. @anttispitkanen Thank you for the workaround

corbinu avatar Mar 29 '22 21:03 corbinu