如何使用Typescript导入moment.js? [英] How can moment.js be imported with typescript?
问题描述
我正在尝试学习打字稿.虽然我认为这无关紧要,但我正在使用VSCode进行演示.
I'm trying to learn Typescript. While I don't think it's relevant, I'm using VSCode for this demo.
我有一个package.json
,其中包含以下内容:
I have a package.json
that has these pieces in it:
{
"devDependencies": {
"gulp": "^3.9.1",
"jspm": "^0.16.33",
"typescript": "^1.8.10"
},
"jspm": {
"moment": "npm:moment@^2.12.0"
}
}
然后我有一个像这样的Typescript类main.js
:
Then I have a Typescript class main.js
like this:
import moment from 'moment';
export class Main {
}
我的gulpfile.js
看起来像这样:
var gulp = require('gulp');
var typescript = require('gulp-tsb');
var compilerOptions = {
"rootDir": "src/",
"sourceMap": true,
"target": "es5",
"module": "amd",
"declaration": false,
"noImplicitAny": false,
"noResolve": true,
"removeComments": true,
"noLib": false,
"emitDecoratorMetadata": true,
"experimentalDecorators": true
};
var typescriptCompiler = typescript.create(compilerOptions);
gulp.task('build', function() {
return gulp.src('/src')
.pipe(typescriptCompiler())
.pipe(gulp.dest('/dest'));
});
当我运行gulp build时,我收到消息:"../main.ts(1,25): Cannot file module 'moment'."
When I run gulp build, I get the message: "../main.ts(1,25): Cannot file module 'moment'."
如果我使用import moment = require('moment');
,则jspm将可以运行并在运行应用程序时引入模块,但仍收到构建错误.
我也尝试过:
If I use import moment = require('moment');
then jspm will work and bring in the module when I run the application, but I'm still receiving the build error.
I also tried:
npm install typings -g
typings install moment --ambient --save
尽管并没有使问题变得更好,反而变得更糟了.现在,我在构建时收到上述错误以及以下错误:"../typings/browser/ambient/moment/index.d.ts(9,21): Cannot find namespace 'moment'."
Instead of making the problem better though, it got worse. Now I get the above error on build as well as the following: "../typings/browser/ambient/moment/index.d.ts(9,21): Cannot find namespace 'moment'."
如果我进入通过键入提供的文件,并在文件底部添加:
If I go to the file provided by typings and add at the bottom of the file:
declare module "moment" { export = moment; }
我可以解决第二个错误,但是我仍然需要require语句才能在我的main.ts
文件中花点时间工作,并且仍然遇到第一个构建错误.
I can get the second error to go away, but I still need the require statement to get moment to work in my main.ts
file and am still getting the first build error.
我是否需要暂时创建自己的.d.ts
文件,或者只是缺少一些设置文件?
Do I need to create my own .d.ts
file for moment or is there just some setup piece I'm missing?
推荐答案
更新
Update
显然,moment现在提供了自己的类型定义(根据 sivabudh 至少从2.14.1开始)您完全不需要typings
或@types
.
Apparently, moment now provides its own type definitions (according to sivabudh at least from 2.14.1 upwards), thus you do not need typings
or @types
at all.
import * as moment from 'moment'
应该加载npm软件包提供的类型定义.
import * as moment from 'moment'
should load the type definitions provided with the npm package.
但是,正如 moment/pull/3319#中所述, issuecomment-263752265 此刻,团队在维护这些定义时似乎遇到了一些问题(他们仍在寻找维护它们的人)..
That said however, as said in moment/pull/3319#issuecomment-263752265 the moment team seems to have some issues in maintaining those definitions (they are still searching someone who maintains them).
您需要安装不带--ambient
标志的moment
类型.
You need to install moment
typings without the --ambient
flag.
然后使用import * as moment from 'moment'
这篇关于如何使用Typescript导入moment.js?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!