Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't create open-ended ranges in TypeScript #264

Open
sarbogast opened this issue Jun 9, 2019 · 3 comments
Open

Can't create open-ended ranges in TypeScript #264

sarbogast opened this issue Jun 9, 2019 · 3 comments

Comments

@sarbogast
Copy link

I'm trying to create open-ended ranges in a TypeScript class, but moment.range() seems to only accept Dates or Moments as inputs, not null or undefined.

import * as Moment from 'moment'
import { extendMoment, MomentRange, DateRange } from 'moment-range'
import {isNullOrUndefined} from "util";

const moment = extendMoment(Moment) as unknown as typeof Moment & MomentRange;

export class Time {
    constructor(
        public startDate: Moment.Moment | undefined,
        public endDate: Moment.Moment | undefined
    ) {}

    overlapsWith(otherTime: Time): DateRange | undefined {
        if((this.startDate === undefined && this.endDate === undefined)
            || (otherTime.startDate === undefined && otherTime.endDate === undefined)) {
            return undefined;
        }

        const thisRange = moment.range(this.startDate, this.endDate);
        const otherRange = moment.range(otherTime.startDate, otherTime.endDate);
        const intersection = thisRange.intersect(otherRange);
        if(intersection === null) {
            return undefined;
        } else {
            return intersection;
        }
    }
}

Those 2 lines fail:

const thisRange = moment.range(this.startDate, this.endDate);
const otherRange = moment.range(otherTime.startDate, otherTime.endDate);
@beaumontjonathan
Copy link
Contributor

Hi @sarbogast, thanks for raising this issue! It does appear to be a bug. I'll get on this shortly 😀

trevorr added a commit to trevorr/moment-range that referenced this issue Mar 5, 2020
@bilottafra
Copy link

I still experience the same problem, but I managed to solve it.
Instead of:
const openRange = moment.range(start, null)
I used:
const openRange = moment.range(start, moment())

In my case logically it had the same effect.

@InesCayollaV
Copy link

When creating open-ended ranges, the problem still persists, it appears that pull-request #273 was intended to solve it, but it was eventually reverted.
Regarding @francescobilotta's answer, using const openRange = moment.range(start, moment()) would create a range from the provided start date up to the current date, rather than an open range. I managed to create an open range with const openRange = moment.range(start) although I had to ignore the TypeScript error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants