sendgrid的打字稿定义

我正在尝试编写一个使用sendgrid的打字稿应用程序,但与其他定义不同,我从打字input中find了typings install sendgrid --ambient一些typings install sendgrid --ambient引起了一些问题:

我能够像这样实例化客户端:

 import * as sendgrid from 'sendgrid'; import Email = Sendgrid.Email; import Instance = Sendgrid.Instance; ... private client: Instance; ... this.client = sendgrid(user, key); 

然后在代码的后面,我试图发送一封电子邮件,这就是为什么ts迫使我导入电子邮件接口的第一位。

 var email = new Email(); ... this.client.send(email, (err, data) => { if (err) { throw err; } }); 

tslint不会抛出任何错误,但是当我构build和运行程序(或者只是我的testing),我得到这个:

摩卡爆炸了! ReferenceError:Sendgrid没有在Object中定义。 (/Users/chrismatic/codingprojects/weview/weview-node/build/server/components/mail/clients/sendgrid.js:4:13)

有没有人有一个工作实施展示,还是我必须写我自己的界面? 先谢谢您的帮助

编辑:

生成的js文件如下所示:

 var sendgrid = require('sendgrid'); var Email = Sendgrid.Email; 

如果我解除“Sendgrid”的资本支出,那么错误消失,但是我不能在ts文件中这样做

这应该给你一个关于如何使用SendGrid的打字稿定义的想法。

 import * as SendGrid from 'sendgrid'; export class SendGridMail extends SendGrid.mail.Mail {} export class SendGridEmail extends SendGrid.mail.Email {} export class SendGridContent extends SendGrid.mail.Content {} export class SendGridService { private sendGrid; constructor(private sendgridApiKey: string) { this.sendGrid = SendGrid(sendgridApiKey); } send(mail: SendGridMail): Promise<any> { let request = this.sendGrid.emptyRequest({ method: 'POST', path: '/v3/mail/send', body: mail.toJSON() }); return this.sendGrid.API(request); } } 

以下是我如何使用上面的类:

 let mail = new SendGridMail( new SendGridEmail('from@example.com'), 'Sending with SendGrid is Fun', new SendGridEmail('to@example.com'), new SendGridContent('text/plain', 'Email sent to to@example.com')); return this.sendgridService.send(mail);