Bart van den Burg
Bart van den Burg

Reputation: 2364

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:

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'

This is my 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?

Upvotes: 0

Views: 967

Answers (2)

Bart van den Burg
Bart van den Burg

Reputation: 2364

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

Upvotes: 2

azyth
azyth

Reputation: 688

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:

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?

Upvotes: 0

Related Questions