Lunigorn Lunigorn - 3 months ago 12
Javascript Question

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

does not contain
field. But this field is actually exists in Javascript code. (actually
is just example, problem exists in many other libs
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 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


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.