简体   繁体   中英

TypeScript to EcmaScript2015 supported by Node4 using gulp

I convert TypeScript to ES2015 (because I need async,await), then convert ES2015 to ES2015 supported by Node4 (Node4 not fully compatible with ES2015).

Problem is that TypeScript definition of express does not contain default field. But this field is actually exists in Javascript code. (actually express is just example, problem exists in many other libs bluebird for example)

So, If I import import {default as express} from "express"; , then I have compilation TypeScript error. If I import as import * as express from "express"; , then I have error when execute express(); like express is not a function in runtime.

Simple project is here .

You need type in console:

npm install
typings install
gulp clean,build
gulp run

to see what I mean. (npm, typescript, gulp should be installed)

How to correct convert TypeScript to ES2015 supported by Node4

The express() function is conceptually the default of the express package. You'd always want to do

import express from "express";

// or this, which is identical, but longer.
import {default as express} from "express";

Using a syntax

import * as express from "express";

will import all of the exports of the module as an object. If you wanted to use that, you'd then have to do

express.default();

The problem with the first example, as you said, is that it can cause TypeScript to throw a type error. The easiest way to fix this is to tell TypeScript to be a little more flexible with it's expectations around what qualifies as a "default" export, when importing a CommonJS module. This is done by enabling the --allowSyntheticDefaultImports option on the TypeScript compiler.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM