本文介绍了TypeScript 1.5:ES6模块默认导入CommonJS'export ='(.d.ts只出现?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个问题:

import moment from 'moment';

时刻本身是一个函数默认的CommonJS导出,如以下编码的:

moment itself is a function that is a default CommonJS export, as coded 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;
}

以下似乎不起作用:

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'm trying to avoid that.

import moment = require('moment');

想法?

推荐答案

您正在寻找的语法

import * as moment from "moment";

这篇关于TypeScript 1.5:ES6模块默认导入CommonJS'export ='(.d.ts只出现?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 05:34