native-dates

API / 函数

kindOf() function

跨 Realm 与多份打包副本识别并收窄 native-dates 值的类型。

import { kindOf, isDateTime } from 'native-dates'core 14.9 kB min+br

读取值的类型标签

kindOf()

kindOf(value):
  | 'datetime' | 'date' | 'time' | 'duration'
  | 'yearMonth' | 'monthDay'
  | 'dateRange' | 'datetimeRange' | 'timeRange'
  | undefined

返回标明值所属类型的字符串标签;对于原生 Date、字符串、数字和普通对象等 JavaScript 值则返回 undefined。可识别具体时刻、日历日期、时钟时间、时长、年月、月日以及范围。

inspect.js
import { date, dateRange, hours, kindOf, monthDay, now, time, yearMonth } from 'native-dates';

kindOf(now()); // 'datetime'
kindOf(date('2026-11-16')); // 'date'
kindOf(time('9:30am')); // 'time'
kindOf(hours(2)); // 'duration'
kindOf(yearMonth('2026-11')); // 'yearMonth'
kindOf(monthDay('10/17')); // 'monthDay'
kindOf(dateRange('2026-11-01', '2026-11-07')); // 'dateRange'
kindOf(new Date()); // undefined
valueunknown必填

要检查的任意值。

'datetime' | 'date' | 'time' | 'duration' | 'yearMonth' | 'monthDay' | 'dateRange' | 'datetimeRange' | 'timeRange' | undefined

表示值类型的字符串标签;当 value 不属于本库时返回 undefined。

类型守卫

类型守卫通过全局符号标识而非原型链来判断类型,因此在跨 Realm 和多份打包副本场景下同样可靠。

isDateTime()

isDateTime(value): value is DateTime

当值为 DateTime 实例时返回 true,并在 TypeScript 中收窄其类型。

import { datetime, isDateTime } from 'native-dates';

const stamp = datetime('2026-11-16T17:30:00Z');
isDateTime(stamp); // true
isDateTime('2026-11-16T17:30:00Z'); // false

isCalendarDate()

isCalendarDate(value): value is CalendarDate

当值为 CalendarDate 实例时返回 true,并在 TypeScript 中收窄其类型。

import { date, isCalendarDate } from 'native-dates';

isCalendarDate(date('2026-11-16')); // true
isCalendarDate(new Date()); // false

isTime()

isTime(value): value is Time

当值为 Time 实例时返回 true,并在 TypeScript 中收窄其类型。

import { isTime, time } from 'native-dates';

isTime(time('9:30am')); // true

isDuration()

isDuration(value): value is Duration

当值为 Duration 实例时返回 true,并在 TypeScript 中收窄其类型。

import { hours, isDuration } from 'native-dates';

isDuration(hours(2)); // true

isYearMonth()

isYearMonth(value): value is YearMonth

当值为 YearMonth 实例时返回 true,并在 TypeScript 中收窄其类型。

import { isYearMonth, yearMonth } from 'native-dates';

isYearMonth(yearMonth('2026-11')); // true

isMonthDay()

isMonthDay(value): value is MonthDay

当值为 MonthDay 实例时返回 true,并在 TypeScript 中收窄其类型。

import { isMonthDay, monthDay } from 'native-dates';

isMonthDay(monthDay('10-17')); // true

isDateRange()

isDateRange(value): value is DateRange

当值为 DateRange 实例时返回 true,并在 TypeScript 中收窄其类型。

import { dateRange, isDateRange } from 'native-dates';

isDateRange(dateRange('2026-11-01', '2026-11-07')); // true

isDateTimeRange()

isDateTimeRange(value): value is DateTimeRange

当值为 DateTimeRange 实例时返回 true,并在 TypeScript 中收窄其类型。

import { datetimeRange, isDateTimeRange } from 'native-dates';

isDateTimeRange(datetimeRange('2026-11-16T09:00Z', '2026-11-16T10:00Z')); // true

isTimeRange()

isTimeRange(value): value is TimeRange

当值为 TimeRange 实例时返回 true,并在 TypeScript 中收窄其类型。

import { isTimeRange, timeRange } from 'native-dates';

isTimeRange(timeRange('9:00am', '5:00pm')); // true

跨 Realm 的 instanceof 与符号标识

标准的 JavaScript instanceof 检查在跨越 iframe 边界或存在重复打包副本时会失效。本库中的每个类都基于全局符号注册表实现了 [Symbol.hasInstance],因此 instanceof 与各类型守卫函数均可跨 Realm 正常工作。

编写自定义序列化器、测试匹配器,或需要通过 IS_RANGE 匹配任意范围类型的通用辅助函数时,也可以直接读取导出的符号标识:

import { DateTime, IS_DATE_TIME, IS_RANGE, now, timeRange } from 'native-dates';

const current = now();
current instanceof DateTime; // true
Boolean(current[IS_DATE_TIME]); // true

const shift = timeRange('9am', '5pm');
Boolean(shift[IS_RANGE]); // true
常量 全局符号键 匹配类型
IS_DATE_TIME Symbol.for('native-dates/DateTime') 匹配任意 DateTime 实例。
IS_CALENDAR_DATE Symbol.for('native-dates/CalendarDate') 匹配任意 CalendarDate 实例。
IS_TIME Symbol.for('native-dates/Time') 匹配任意 Time 实例。
IS_DURATION Symbol.for('native-dates/Duration') 匹配任意 Duration 实例。
IS_YEAR_MONTH Symbol.for('native-dates/YearMonth') 匹配 YearMonth 实例。
IS_MONTH_DAY Symbol.for('native-dates/MonthDay') 匹配 MonthDay 实例。
IS_RANGE Symbol.for('native-dates/Range') 匹配 DateRange、DateTimeRange 与 TimeRange 实例。
IS_DATE_RANGE Symbol.for('native-dates/DateRange') 匹配 DateRange 实例。
IS_DATE_TIME_RANGE Symbol.for('native-dates/DateTimeRange') 匹配 DateTimeRange 实例。
IS_TIME_RANGE Symbol.for('native-dates/TimeRange') 匹配 TimeRange 实例。
编辑此页更新于 2026年11月12日