sendgrid-nodejs
sendgrid-nodejs copied to clipboard
Sendgrid with TypeScript > 4.4
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
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.
Any update on this? Or potential mitigation? This is essentially blocking upgrading to TypeScript >=4.4 👀
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.
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? 👀
Just got bit by this and it seems a pretty breaking issue for it to simply be backlogged. @anttispitkanen Thank you for the workaround