我尝试使用它与typescript绑定:

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

test.ts:

import {moment} from 'moment/moment';

如果没有:

npm install moment --save

test.ts:

var moment = require('moment/moment');

但是当我调用moment.format()时,我得到一个错误。 应该是简单的,谁能提供一个命令行/导入组合,将工作?


当前回答

在systemjs中,我所做的是。我添加了map

map: {
   .....,
   'moment': 'node_modules/moment/moment.js',
   .....
}

然后你可以很容易地导入力矩

import * as moment from 'moment'

其他回答

除了SnareChop的答案,我还必须更改momentjs的类型文件。

在moment-node.d。我替换的ts:

export = moment;

with

export default moment;

下面的方法对我很有效。

首先,安装moment的类型定义。

类型安装时刻—保存

(注:NOT -ambient)

然后,为了解决缺乏适当的导出:

import * as moment from 'moment';

要在Typescript项目中使用它,你需要安装moment:

NPM安装时刻——保存

在安装moment之后,你可以在导入后的任何.ts文件中使用它:

Import * as moment from "moment";

对于ANGULAR CLI用户

使用外部库在这里的文档中:

https://github.com/angular/angular-cli/wiki/stories-third-party-lib

Simply install your library via npm install lib-name --save and import it in your code. If the library does not include typings, you can install them using: npm install lib-name --save npm install @types/lib-name --save-dev Then open src/tsconfig.app.json and add it to the types array: "types":[ "lib-name" ] If the library you added typings for is only to be used on your e2e tests, instead use e2e/tsconfig.e2e.json. The same goes for unit tests and src/tsconfig.spec.json. If the library doesn't have typings available at @types/, you can still use it by manually adding typings for it: First, create a typings.d.ts file in your src/ folder. This file will be automatically included as global type definition. Then, in src/typings.d.ts, add the following code: declare module 'typeless-package'; Finally, in the component or file that uses the library, add the following code: import * as typelessPackage from 'typeless-package'; typelessPackage.method(); Done. Note: you might need or find useful to define more typings for the library that you're trying to use.

2017年4月更新:

从版本2.13.0开始,Moment包含了一个typescript定义文件。https://momentjs.com/docs/#/use-it/typescript/

只要用npm在你的控制台类型中安装它

npm install --save moment

然后在你的Angular应用中,导入就像这样简单:

import * as moment from 'moment';

就是这样,你得到了完全的Typescript支持!

额外编辑:要在Typescript中输入一个变量或属性Moment,你可以这样做,例如:

let myMoment: moment.Moment = moment("someDate");