sendgrid-nodejs
sendgrid-nodejs copied to clipboard
export type MailDataRequired generate errors in arguments (Typescript)
When use send function in typescript, this generate a error Argument for types (specifically with content resolution)
Issue Summary
Steps to Reproduce
Code Snippet
import MailService from '@sendgrid/mail';
...
const emailOpts = {
to: [<your email here>],
from : <verified email in sendrid account for send emails>,
subject: 'Sending with Twilio SendGrid is Fun',
text: 'and easy to do anywhere, even with Node.js',
html: '<strong>and easy to do anywhere, even with Node.js</strong>'
}
serviceResp = await MailService.send(emailOpts);
...
Exception/Log
Argument of type '(MailData & { text: string; }) | (MailData & { html: string; }) | (MailData & { templateId: string; }) | (MailData & { content: MailContent[] & { ...; }; })' is not assignable to parameter of type 'MailDataRequired'.
Property 'content' is missing in type 'EmailInput' but required in type '{ content: MailContent[] & { 0: MailContent; }; }'.
Additional log in debug:

Technical details:
- sendgrid-nodejs version: "^7.2.1"
- node version: 14.4.0
Hi @jaun-rg
I was able to reproduce this bug. 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.
Hello ! I have the same issue. Do you have a temporary solution while waiting for a fix ?
Hi, Has this issue been resolved yet, seems like this thread has become stale.
Hi, is there any progress on this? Even the type definitions file mail.d.ts has broken imports in it, so I can't see type definitions for what is required for me to send mail.
I tried to install @types/sendgrid and it installed an empty package:
This is a stub types definition for sendgrid (https://github.com/sendgrid/sendgrid-nodejs).
sendgrid provides its own type definitions, so you don't need @types/sendgrid installed!
I'm unable to import any types from the current package.
Same problem here. I cannot work out how to get typings for the "msg" object in sgMail.send(msg). I've looked absolutely everywhere and the solution posted elsewhere which says to use
import * as SendGrid from '@sendgrid/mail' const msg: typeof SendGrid.MailService = {...}
soesn't work because the Sendgrid object doesn't export a member called MailService.
@jon64digital try:
import { MailDataRequired } from '@sendgrid/helpers/classes/mail';
const msg: MailDataRequired = {...}
Also, fwiw:
import * as MailService from '@sendgrid/mail';
MailService.setApiKey('...');
...
const response = await MailService.send(msg);
Hi,
I have an issue using the MailDataRequired type. I can't use the second option (mentioned in the below document) with typescript. https://github.com/sendgrid/sendgrid-nodejs/blob/main/docs/use-cases/transactional-templates.md#prevent-escaping-characters.
I had the same problem, where a MailDataRequired is required by the send function. I'm working with a template id and template data, so I don't need the content node. I used the type MailData and I cast it to <any> as a temporary workaround.
// need @sendgrid/mail and @sendgrid/helpers installed
import sendgrid from '@sendgrid/mail';
import { MailData } from '@sendgrid/helpers/classes/mail';
// this is simplified, but there is no `content` node.
const payload: MailData = this.getPayload();
await sendgrid.send(<any>payload);
The function should take a MailData (MailDataRequired fits this interface), OR possibly split functionality across multiple methods-- one for send with a template and one for send with the content provided.
This annoying bug keeps going to Typescript coders 😞