打字稿编译不会对某些文件添加require语句 [英] Typescript compilation doesn't add require statement to some files
问题描述
我正在开发一个非常简单的将nodejs项目转换为typescript。节点项目的前2个/主要文件正确翻译。第三个文件除了需要在文件的顶部的语句拉入依赖。
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.
我确信找到依赖关系,因为VSCode可以自动完成和着色依赖对象,没有编译器错误。这3个文件中的每个文件使用ts import语句,而不是必需的。
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.
由于它在3个文件中的2个文件中工作,它们基本相同,我不知道开始。这听起来像一个编译问题?
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;
推荐答案
您的问题是,输出:
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.
类似下面的工作方式:
import * as sendgrid from "sendgrid";
const _ensure = sendgrid;
更多
https://basarat.gitbooks.io/typescript/content/docs /project/external-modules.html
这篇关于打字稿编译不会对某些文件添加require语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!