如何使用打字稿导入moment.js?

pei*_*ent 71 momentjs typescript jspm

我正在尝试学习Typescript.虽然我认为它不相关,但我正在使用VSCode进行此演示.

我有一个package.json包含这些内容的部分:

{
  "devDependencies": {
    "gulp": "^3.9.1",
    "jspm": "^0.16.33",
    "typescript": "^1.8.10"
  },
  "jspm": {
    "moment": "npm:moment@^2.12.0"
  }
}
Run Code Online (Sandbox Code Playgroud)

然后我有一个这样的Typescript类main.js:

import moment from 'moment';
export class Main {
}
Run Code Online (Sandbox Code Playgroud)

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'));
});
Run Code Online (Sandbox Code Playgroud)

当我运行gulp build时,我收到消息: "../main.ts(1,25): Cannot file module 'moment'."

如果我使用import moment = require('moment');那么jspm将工作并在我运行应用程序时引入模块,但我仍然收到构建错误.我也尝试过:

npm install typings -g
typings install moment --ambient --save
Run Code Online (Sandbox Code Playgroud)

它没有让问题变得更好,而是变得更糟.现在我在构建时遇到以上错误以及以下错误:"../typings/browser/ambient/moment/index.d.ts(9,21): Cannot find namespace 'moment'."

如果我转到typings提供的文件并添加到文件的底部:

declare module "moment" { export = moment; }
Run Code Online (Sandbox Code Playgroud)

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

我是否需要.d.ts暂时创建自己的文件,或者只是我缺少一些设置文件?

Aid*_*des 89

更新

显然,时刻现在提供了自己的类型定义(根据sivabudh至少从2.14.1向上),因此你根本不需要typings或根本不需要@types.

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

尽管如此,正如在/ moment/3319#issuecomment-263752265中说的那样,团队似乎在维护这些定义方面存在一些问题(他们仍在寻找维护它们的人).


你需要安装moment没有--ambient旗帜的打字.

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


小智 49

你需要在TS中分别导入moment()函数和Moment类.

我在这里的打字稿文档中找到了一个注释.

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

因此,将时刻js导入typescript的代码实际上如下所示:

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

  • 宾果游戏-希望我能使这个成为公认的答案 (3认同)
  • 现在这可以用作`import*as moment from'moment';`然后使用`moment.Moment`作为打字. (3认同)

siv*_*udh 14

通过打字

Moment.js现在支持v2.14.1中的TypeScript.

请参阅:https://github.com/moment/moment/pull/3280


可能不是最好的答案,但这是蛮力的方式,它对我有用.

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

$ tree . ??? main.js ??? main.js.map ??? main.ts ??? moment.js

  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'));
Run Code Online (Sandbox Code Playgroud)
  1. 只是node用来跑main.js.


NSj*_*nas 12

不知道什么时候改变了,但是使用最新版本的打字稿,你只需要使用import moment from 'moment';,其他一切都应该正常工作.

  • `import * as from'moment'的瞬间不适用打字稿,但是`import from from'moment'的瞬间却可以。 (4认同)

har*_*ula 5

ES6语法:

1.安装时刻

npm install --save moment
Run Code Online (Sandbox Code Playgroud)

2. 在你的打字稿文件中导入“时刻”

import moment from 'moment';
Run Code Online (Sandbox Code Playgroud)

  • 这不是在打字稿中导入模块的正确方法。请考虑更新您的答案。请参阅此进行确认:https://basarat.gitbook.io/typescript/project/modules/external-modules (2认同)

Mar*_*eck 5

我刚刚注意到我赞成和评论的答案是模棱两可的。所以以下正是对我有用的。我目前在 Moment2.26.0和 TS 上3.8.3

在代码中:

import moment from 'moment';
Run Code Online (Sandbox Code Playgroud)

在 TS 配置中:

{
  "compilerOptions": {
    "esModuleInterop": true,
    ...
  }
}
Run Code Online (Sandbox Code Playgroud)

我建立了所以这个配置导入到其他配置文件CommonJS的和EMS。

洞察力来自与使用 Express 相关的答案。我认为值得在这里添加,以帮助任何与 Moment.js 相关的搜索,而不是更一般的东西。