【发布时间】:2021-06-27 20:10:07
【问题描述】:
我正在尝试学习 Typescript。虽然我认为这无关紧要,但我在此演示中使用 VSCode。
我有一个package.json,里面有这些片段:
{
"devDependencies": {
"gulp": "^3.9.1",
"jspm": "^0.16.33",
"typescript": "^1.8.10"
},
"jspm": {
"moment": "npm:moment@^2.12.0"
}
}
然后我有一个 Typescript 类 main.js 像这样:
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'."
如果我使用import moment = require('moment');,那么 jspm 将在我运行应用程序时工作并引入模块,但我仍然收到构建错误。
我也试过了:
npm install typings -g
typings install moment --ambient --save
问题不但没有改善,反而变得更糟。现在我在构建时收到上述错误以及以下错误:"../typings/browser/ambient/moment/index.d.ts(9,21): Cannot find namespace 'moment'."
如果我转到typings提供的文件并在文件底部添加:
declare module "moment" { export = moment; }
我可以让第二个错误消失,但我仍然需要 require 语句才能在我的 main.ts 文件中工作,并且仍然遇到第一个构建错误。
我需要暂时创建自己的.d.ts 文件还是缺少一些设置?
【问题讨论】:
-
为现在遇到此问题的任何人添加此更新:
import moment, { Moment } from 'moment';允许您执行const x = moment();和const x: Moment = moment(); -
这些解决方案都不起作用,我搬到了这里 - github.com/date-fns/date-fns
标签: typescript momentjs jspm