0

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;
DFBerry
  • 1,818
  • 1
  • 19
  • 37
  • 1
    In this 3rd file, are the imported variables that were removed used in the code? – Nitzan Tomer Sep 04 '16 at 14:11
  • 1
    Could you post some code? At least the syntax of the imports in file 3 – Paarth Sep 04 '16 at 15:59
  • I've narrowed it down to the compiler doesn't think the code is related to sendgrid so it removes that import statement. I'm not sure why VSCode can find it for intellisense but tsc can't. – DFBerry Sep 04 '16 at 22:30
  • Thanks Nitzan and Paarth, while you didn't give me the answer, you gave me enough clues to know what to focus on. The import statement uses a lowercase s while the interior code uses a captial S. That was the issue. Once I changed the import statement to use a capital S, then the code matched the import and the reference was compiled to the js file. – DFBerry Sep 04 '16 at 22:47

1 Answers1

0

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

basarat
  • 261,912
  • 58
  • 460
  • 511