【发布时间】:2016-07-11 00:06:26
【问题描述】:
我通过打字安装了时刻定义文件:
typings install moment --save
到目前为止一切顺利,但是当我通过以下方式导入定义时:
import * as moment from 'moment';
所有编译都很好,但是当我运行我的应用程序时,我收到了运行时错误:
Uncaught TypeError: moment is not a function
moment库究竟应该如何导入?
如果我改为使用:
const moment = require('moment');
一切正常,但我失去了所有类型安全性。
我的tsconfig.json:
{
"compilerOptions": {
"jsx": "preserve",
"target": "ES6",
"declaration": false,
"module": "es2015",
"experimentalDecorators": true,
"sourceMap": true,
"noLib": false,
},
"exclude": [
"node_modules",
"meteor_core",
"typings/browser",
"typings/browser.d.ts"
]
}
任何帮助将不胜感激。
【问题讨论】:
-
你如何引用/导入你的定义文件(打字文件夹)?
-
请发布您的
tsconfig.json。 -
引用是由typings模块完成的,它创建了两个主要的定义文件:broswer.d.ts和main.d.ts。对于所有其他提供类型的 npm 包,它可以正常工作。