我正在进行一个非常简单的nodejs项目转换为typescript。节点项目的前2个/主文件已正确转换。除了文件顶部的requires语句以引入依赖关系之外,第三个被转换。
我确定找到了依赖项,因为VSCode可以自动完成并为依赖项对象着色,并且没有编译器错误。 3个文件中的每一个都使用ts import语句而不是require。
因为它适用于3个文件中的2个并且它们基本相同,所以我不知道从哪里开始。这听起来像编译问题吗?
//tsconfig.json
{
"compileOnSave": true,
"compilerOptions": {
"module": "commonjs",
"sourceMap": true,
"target": "es6",
"moduleResolution": "node",
"allowUnreachableCode" : true,
"listFiles":true,
"outDir":"js",
"watch": true
},
"files": [
"./typings/index.d.ts",
"./src/route.ts",
"./src/app.ts",
"./src/email.ts",
"./spec/email.spec.ts"
]
}
//email.ts
"use strict";
import * as sendgrid from "sendgrid";
export class mailer{
public Send(){
let sendGridAcctUser = "dina@dfberry.io";
let sendGridAcctKey: string = "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";
let toEmail = "admin@dfberry.io";
let fromEmail = "dinaberry@outlook.com";
let mysubject = "test email";
let mytext = "this is a test";
let options: Sendgrid.EmailOptions;
options.subject = mysubject;
options.text = mytext;
options.to = toEmail;
options.from = fromEmail;
options.replyto = fromEmail;
let myEmail: Sendgrid.Email;
myEmail = new Sendgrid.Email(options);
//let emailer : SendGrid;
let sg : Sendgrid.Constructor;
let sgInstance : Sendgrid.Instance;
sgInstance = new sg(sendGridAcctUser, sendGridAcctKey);
sgInstance.send(options, function( err, json){
console.log(err);
console.log(json);
} );
}
}
// compiled email.js -- expect requires after "use strict"
"use strict";
class mailer {
Send() {
let sendGridAcctUser = "dina@dfberry.io";
let sendGridAcctKey = "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";
let toEmail = "admin@dfberry.io";
let fromEmail = "dinaberry@outlook.com";
let mysubject = "test email";
let mytext = "this is a test";
let options;
options.subject = mysubject;
options.text = mytext;
options.to = toEmail;
options.from = fromEmail;
options.replyto = fromEmail;
let myEmail;
myEmail = new Sendgrid.Email(options);
//let emailer : SendGrid;
let sg;
let sgInstance;
sgInstance = new sg(sendGridAcctUser, sendGridAcctKey);
sgInstance.send(options, function (err, json) {
console.log(err);
console.log(json);
});
}
}
exports.mailer = mailer;
答案 0 :(得分:0)
您的问题是以下行已从输出中删除:
import * as sendgrid from "sendgrid";
这是可以预料的。如果在变量位置的文件中未使用导入,则将其删除。
以下内容将起作用:
import * as sendgrid from "sendgrid";
const _ensure = sendgrid;
确保导入包含在https://basarat.gitbooks.io/typescript/content/docs/project/external-modules.html
中