Skip to content

defineIntervalType

function defineIntervalType<T>(domain): BoundIntervalType<T>;

Defined in: interval.ts:869

Bind a IntervalDomain to a concrete, new-able interval CLASS that extends Interval, so instances are instanceof Interval (and instanceof the bound class). The built-in classes (NumberInterval, DateInterval, …) are made this way; use it for your own domains.

Operations inherited from Interval (union, intersection, …) return base Interval<T> values (still instanceof Interval, but not instanceof the bound class).

Type Parameter
T
Parameter Type
domain IntervalDomain<T>

BoundIntervalType<T>

import { defineIntervalType, Interval, numberDomain } from "@xtandard/lib/interval";
const PercentInterval = defineIntervalType({
...numberDomain,
name: "percent",
isValid: (v: number) => v >= 0 && v <= 100,
});
type PercentInterval = InstanceType<typeof PercentInterval>;
const r = new PercentInterval({ start: 20, startClose: true, end: 80, endClose: true });
r.toString(); // "[20,80]"
r instanceof PercentInterval; // true
r instanceof Interval; // true
PercentInterval.atLeast(50).contains(150); // false (fails isValid)
PercentInterval.parse("[0,100)").toString(); // "[0,100)"