首页 文章

无法使用MomentInput类型冲突构建moment.js打字稿

提问于
浏览
2

我有以下打字稿代码

import * as moment from "moment"

const months = this.getMonths();
const firstMonth = moment.min(months).startOf("month");
const lastMonth = moment.max(months).startOf("month");

public getMonths(): Array<moment.Moment> {
    const assignmentMonths = _.map(this.internalBudget.categoryAssignments, a => a.month);
    const transactionMonths = _.map(this.internalBudget.transactions, a => a.date);

    return _.concat(assignmentMonths, transactionMonths);
}

这是在运行tsc时为moment.min和moment.max行抛出以下错误 .

TS2345: Argument of type 'Moment[]' is not assignable to parameter of type 'MomentInput'.
  Type 'Moment[]' has no properties in common with type 'MomentInputObject'.

我的tsconfig.json文件是

{
  "compilerOptions": {
    "moduleResolution": "node",
    "target": "es6",
    "sourceMap": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "skipDefaultLibCheck": true,
    "lib": [ "es6", "dom" ],
    "types": [ "node", "jasmine" ]
  },
  "exclude": [ "bin", "node_modules" ],
  "atom": { "rewriteTsconfig": false }
}

我在文档中找不到提及MomentInput对象的任何内容,或者弄清楚如何正确编译它 . 从瞬间开始,似乎我应该能够将一个Moments数组传递给min / max,但我明白这只是javascript,并且在该级别没有打字稿的概念 .

我试图构造一个MomentInput对象,但其意图似乎只是片刻而不是数组 . 如何构建这个?一切的版本是:

  • 打字稿:2.4.1

  • 节点:6.11.0

  • Npm:3.10.10

2 回答

  • 3

    我通过像这样使用扩展运算符来实现它:而不是 moment.min(dates) 我使用 moment.min(...dates)

  • 0

    降级到Typescript 2.3.4使一切正常 . 我没有意识到2.4.1刚刚在我的问题发布前几个小时发布,所以现在可能还有一些问题 .

相关问题