问题
I'm working on a very simple conversion of a nodejs project to typescript. The first 2/main files of the node project transpiled correctly. The 3rd transpiled except for the requires statement at the top of the file to pull in dependencies.
I'm sure the dependency is found because VSCode could autocomplete and color the dependency objects and there were no compiler errors. Each of the 3 files uses ts import statements instead of requires.
Since it works in 2 of the 3 files and they are basically the same, I don't know where to start. Does this sound like a compile issue?
//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;
回答1:
Your issue is that the following line is getting removed from the output:
import * as sendgrid from "sendgrid";
This is to be expected. If an import is not used in the file in a variable position then its erased.
Fix:
Something like the following will work:
import * as sendgrid from "sendgrid";
const _ensure = sendgrid;
More
Ensure import is covered here https://basarat.gitbooks.io/typescript/content/docs/project/external-modules.html
来源:https://stackoverflow.com/questions/39317786/typescript-compilation-doesnt-add-require-statement-to-some-files