TypeScript 1.5: default import of ES6 module in CommonJS 'export =' (only .d.ts)?

I ran into a problem:

import moment from 'moment'; 

moment itself is a function that is a standard CommonJS export, as encoded here https://github.com/borisyankov/DefinitelyTyped/blob/master/moment/moment.d.ts :

 interface MomentStatic { (): Moment; (date: number): Moment; ... } declare var moment: moment.MomentStatic; declare module 'moment' { export = moment; } 

The following do not seem to work:

 import * from 'moment'; // error TS1005: 'as' expected. // error TS1005: 'from' expected. import moment from 'moment'; // error TS1192: External module ''moment'' has no default export. import {default as moment} from 'moment'; // error TS2305: Module ''moment'' has no exported member 'default'. 

The require syntax still works ... but I try to avoid this.

 import moment = require('moment'); 

Thoughts?

+5
source share
1 answer

The syntax you are looking for

 import * as moment from "moment"; 
+19
source

All Articles