问题描述
我正在尝试学习打字稿.虽然我认为这无关紧要,但我在此演示中使用了 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
看起来像这样:
My gulpfile.js
looks like this:
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): 找不到命名空间 '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'."
如果我转到typings提供的文件并在文件底部添加:
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?
推荐答案
更新
显然,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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!