简体   繁体   English

矩范围插件的打字稿错误

[英]Typescript error with moment range plugin

I have included moment js and the moment-range plugin in my Ionic 2 app like so: 我在Ionic 2应用程序中包含了moment jsmoment-range插件,如下所示:

import * as moment from 'moment';
import 'moment-range';

This works fine and I can use them both, but Typescript gives me the following error: 这工作正常,我可以同时使用它们,但是Typescript给我以下错误:

Javascript: Javascript:

let range = moment().range(self.weekStart, self.weekEnd);

Typescript error: 打字稿错误:

Error TS2339: Property 'range' does not exist on type 'Moment'.

I have ran the following command to try and stop this error by installing a typings file: 我已经运行以下命令来尝试通过安装打字文件来停止此错误:

typings install moment-range --ambient --save

but it doesn't seem to have had any effect. 但这似乎没有任何效果。 Is there something else I need to do or is there a way of silencing the error? 还有什么我需要做的,还是有一种可以消除错误的方法?

Thanks for any help. 谢谢你的帮助。

I had to do this: 我必须这样做:

import * as moment from "moment";
import {extendMoment} from "moment-range";
const rangeMoment = extendMoment(moment);
const range = rangeMoment.range(start, end);

I just looked at the interfaces. 我只是看着界面。 You need to call 你需要打电话

let range = moment.range(self.weekStart, self.weekEnd);

not moment().range 不是moment().range

If you look at moment-range.d.ts you'll see that the range method is defined on the static interface MomentStatic , not the instance interface Moment . 如果查看一下moment-range.d.ts,您会看到range方法是在静态接口MomentStatic而不是实例接口Moment

I could not get Paarth solution to work. 我无法使用Paarth解决方案。 I still got: 我仍然得到:

Property 'range' does not exist on type 'typeof moment'.

The solution for me was to use type assertion: 对我来说,解决方案是使用类型断言:

let range = (<any>moment).range(startDate, endDate);

import * as moment from 'moment';
import { extendMoment } from 'moment-range';

const { range } = extendMoment(moment);

const timeRange = range(moment.utc([2015, 0, 1]), moment.utc([2015, 5, 1]));

for (const month of timeRange.by('month')) {
  console.log(month.format('YYYY-MM-DD'));
}

They only way I got this to work in "typescript": "3.4.5" 他们只有这样才能让我在“打字稿”中工作:“ 3.4.5”

const Moment = require('moment');
import {extendMoment} from 'moment-range';

const moment = extendMoment(Moment);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM