简体   繁体   English

在打字稿项目中包括瞬间范围的打字

[英]Including moment-range typings in typescript project

I've recently started a new typescript project and now I want to make use of moment-range. 我最近开始了一个新的打字稿项目,现在我想利用矩量范围。 I've installed moment-range and @typings/moment-range and I've added these lines to the top of my document: 我已经安装了moment-range和@ typings / moment-range,并将这些行添加到了文档的顶部:

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

However, I'm still presented with this error: Property 'range' does not exist on type 'typeof moment' 但是,我仍然遇到此错误: Property 'range' does not exist on type 'typeof moment'

This is my tsconfig.json: 这是我的tsconfig.json:

{
    "compilerOptions": {
        "module": "commonjs",
        "target": "es5",
        "experimentalDecorators": true,
        "emitDecoratorMetadata": true,
        "noEmitHelpers": false,
        "noEmitOnError": true,
        "lib": [
            "es6",
            "dom",
            "es2015.iterable"
        ],
        "baseUrl": ".",
        "paths": {
            "*": [
                "./node_modules/tns-core-modules/*",
                "./node_modules/*"
            ]
        }
    },
    "exclude": [
        "node_modules",
        "platforms",
        "**/*.aot.ts"
    ]
}

What step am I missing? 我想走哪一步?

Also, is there a way to globally include the moment range typings so that I don't have to do it in each file? 另外,是否有一种方法可以全局地包括弯矩范围类型,这样我就不必在每个文件中都这样做呢?

Ok, I figured it out. 好的,我知道了。 I created a file like this: 我创建了一个像这样的文件:

import * as moment from "moment";
import { DateRange } from 'moment-range';

declare module "moment" {
    function range(range: string | Date[] | moment.Moment[]): DateRange;
    function range(start: Date | moment.Moment, end: Date | moment.Moment): DateRange;
}

I referenced it once in my main file and now I can use moment.range() anywhere 我在主文件中引用了一次,现在可以在任何地方使用moment.range()

It looks like you are trying to call range on moment itself. 看来您正在尝试即时调用范围。 instead of moment-range. 而不是瞬间范围。

if you look at their Docs on npmjs it says to import moment range like this: 如果您在npmjs上查看他们的文档,则说要导入力矩范围,如下所示:

import Moment from 'moment';
import { extendMoment } from 'moment-range';

const moment = extendMoment(Moment);

Are you extending moment? 你在延长时间吗? also what kind of project are you operating in? 您还在从事哪种项目?

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

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