Skip to content

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

@sarbogast

Description

@sarbogast

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);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions