如何使用打印脚本导入 Moment.js?

我正在学打字机。虽然我不认为这是相关的,但我正在使用 VSCode 为这个演示。

我有一个 package.json里面有这些片段:

{
"devDependencies": {
"gulp": "^3.9.1",
"jspm": "^0.16.33",
"typescript": "^1.8.10"
},
"jspm": {
"moment": "npm:moment@^2.12.0"
}
}

然后我有一个类 main.js,像这样:

import moment from 'moment';
export class Main {
}

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'));
});

When I run gulp build, I get the message: "../main.ts(1,25): Cannot file module 'moment'."

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. 我也试过:

npm install typings -g
typings install moment --ambient --save

然而,问题并没有得到改善,反而变得更糟了。现在我得到了构建时的上述错误以及以下错误: "../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; }

我可以得到第二个错误消失,但我仍然需要的需求语句,以获得在我的 main.ts文件的工作时刻,我仍然得到第一个构建错误。

我需要创建我自己的 .d.ts文件的时刻还是只是一些安装片段我错过了?

155497 次浏览

更新

显然,moment 现在提供了自己的类型定义(至少从2.14.1开始) ,因此根本不需要 typings@types

import * as moment from 'moment'应该加载 npm 包提供的类型定义。

尽管如此,正如在 时刻/拉/3319 # 问题建议 -263752265中所说的那样,团队在维护这些定义时似乎遇到了一些问题(他们仍在搜索维护这些定义的人)。


您需要安装没有 --ambient标志的 moment类型。

然后使用 import * as moment from 'moment'包含它

通过打字

Js 现在在 v2.14.1中支持 TypeScript。

见: https://github.com/moment/moment/pull/3280


直接

也许不是最好的答案,但这是蛮力的方式,对我很有效。

  1. 只需下载实际的 moment.js文件并将其包含在您的项目中。
  2. 例如,我的项目是这样的:

$树 . 纪念品 ├── main.js.map ├── main.ts - 凶狠的-瞬间 J

  1. 下面是一个源代码示例:

```

import * as moment from 'moment';


class HelloWorld {
public hello(input:string):string {
if (input === '') {
return "Hello, World!";
}
else {
return "Hello, " + input + "!";
}
}
}


let h = new HelloWorld();
console.log(moment().format('YYYY-MM-DD HH:mm:ss'));
  1. 只要使用 node运行 main.js

您需要在 TS 中分别导入函数 时刻()和类 等一下

我在打印文档 给你中发现了一个注释。

/* ~ 注意,ES6模块不能直接导出可调用函数
* ~ 此文件应使用 CommonJS 风格导入:
*~ import x = require('someLibrary');

所以将 moment js 导入到类型脚本的代码实际上看起来是这样的:

import { Moment } from 'moment'
....
let moment = require('moment');
...
interface SomeTime {
aMoment: Moment,
}
...
fn() {
...
someTime.aMoment = moment(...);
...
}

不确定这是什么时候改变的,但是使用最新版本的打印脚本,您只需要使用 import moment from 'moment';,其他一切都应该正常工作。

更新:

看起来最近修复了他们的进口。至少从 2.24.0开始你会想要使用 import * as moment from 'moment';

ES6 syntax:

1. install moment

npm install --save moment

2. Import "moment" in your typescript file

import moment from 'moment';

我刚刚注意到,我所支持和评论的答案是模棱两可的。所以下面的方法对我很有效。我现在正在播放《时刻 2.26.0》和《时刻 3.8.3

In code:

import moment from 'moment';

在 TS 配置中:

{
"compilerOptions": {
"esModuleInterop": true,
...
}
}

我正在为 都有 CommonJS 和 EMS 构建,因此这个配置被导入到其他配置文件中。

这个见解来自于与使用 Express 相关的 这个答案。不过,我认为这里值得添加一些东西,以帮助任何搜索 Moment.js 的人,而不是更一般的东西。

还是坏了? 试试卸载 @types/moment

So, I removed @types/moment package from the package.json file and it worked using:

import * as moment from 'moment'

新版本的 moment不需要 @types/moment包,因为已经包含了类型。