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

export type MailDataRequired generate errors in arguments (Typescript)

Open jaun-rg opened this issue 5 years ago • 9 comments

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: Screen Shot 2020-07-20 at 20 50 09

Technical details:

  • sendgrid-nodejs version: "^7.2.1"
  • node version: 14.4.0

jaun-rg avatar Jul 21 '20 01:07 jaun-rg

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.

eshanholtz avatar Jul 24 '20 21:07 eshanholtz

Hello ! I have the same issue. Do you have a temporary solution while waiting for a fix ?

thibaultadet avatar Oct 30 '20 16:10 thibaultadet

Hi, Has this issue been resolved yet, seems like this thread has become stale.

FMathemera avatar Jan 14 '21 09:01 FMathemera

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.

inorganik avatar Jan 24 '21 13:01 inorganik

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.

JonCognioDigital avatar Feb 07 '21 17:02 JonCognioDigital

@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);

ilionic avatar Feb 18 '21 17:02 ilionic

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.

syJSdev avatar Apr 14 '21 08:04 syJSdev

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.

ps2goat avatar May 06 '21 21:05 ps2goat

This annoying bug keeps going to Typescript coders 😞

Tokenyet avatar Oct 18 '21 06:10 Tokenyet