3

I'm trying to get this to work in VSCode, but I can't seem to find a solution anywhere. I just want to use Generator in Typescript with VSCode, but i found that the nodejs can't support keyword "import" . If use "ES5" as target in tsconfig.json, typescript can't support Generator in VSCode. Here is demo:

// typescript app.ts
import * as http from "http";

http.createServer((request, response) => {  
    response.writeHead(200, {"Content-Type": "text/html"});  
    response.write("Hello World!");  
    response.end();
}).listen(8000);  

And then get this

/// <reference path="typings/tsd.d.ts" />
import * as http from "http";
http.createServer((request, response) => {
    response.writeHead(200, { "Content-Type": "text/html" });
    response.write("Hello World!");
    response.end();
}).listen(8000);
//# sourceMappingURL=app.js.map

run by node --harmony app.js or node --harmony_modules app.js all get:

g:\Temp\TypeScriptTest>node --harmony app.js
g:\Temp\TypeScriptTest\app.js:2
import * as http from "http";
^^^^^^

SyntaxError: Unexpected reserved word
    at exports.runInThisContext (vm.js:53:16)
    at Module._compile (module.js:413:25)
    at Object.Module._extensions..js (module.js:452:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Function.Module.runMain (module.js:475:10)
    at startup (node.js:117:18)
    at node.js:951:3

Or use

import http = require("http"); // ES5 require module style

VSCode warning

Import assignment cannot be used when targeting ECMAScript 6 or higher. Consider using 'import * as ns from "mod"', 'import {a} from "mod"' or 'import d from "mod"' instead.

Has some way to typescript compile *.ts file use ES6 style import module and I can use ES6 features such as Generator in Typescript?

4
  • The node version is 4.1.2 Commented Oct 17, 2015 at 4:04
  • I think you need to drop one feature, for now or may be try babel like ts -> es6 -> babel? Commented Oct 17, 2015 at 4:06
  • Thanks for your answer. That is one way to resolved, and then I can write one tool to replace all the import statement to var ns = require("mod") after tsc compile the *.ts. Commented Oct 17, 2015 at 4:23
  • Both Nodejs 4.1.x and Typescript support some ES6 features/syntactic, tsc compile *.ts in es6 can't run in Node will make people feel very tangled, Especially the beginning of the import statement Commented Oct 17, 2015 at 4:29

1 Answer 1

1

Has some way to typescript compile *.ts file use ES6 style import module and I can use ES6 features such as Generator in Typescript?

You need to target es6 and then pass the output through babel before running it in node.

One way to do that is to use the externalTranspiler feature of atom-typescript documented here : https://github.com/TypeStrong/atom-typescript/blob/master/docs/faq.md#can-i-use-an-alternate-transpiler

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I will try this~~

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.