2015-04-12 96 views
5

我現在遇到的問題與ES6模塊默認導入:打字稿1.5:CommonJS的「出口=」(?.d.ts唯一的問題)

import moment from 'moment'; 

moment本身就是一個功能是默認CommonJS的出口,這裏編碼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; 
} 

下似乎不工作:

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'. 

require語法仍然有效......但我試圖避免這種情況。

import moment = require('moment'); 

想法?

+0

鏈接複製的語法? –

回答

18

您正在尋找

import * as moment from "moment"; 
+0

適合我,謝謝! –

+1

對不起,這是不正確的,請參閱http://stackoverflow.com/a/29598404/252087 –

+0

謝謝。我已要求澄清:https://github.com/Microsoft/TypeScript/issues/2242#issuecomment-92218146 – basarat