【发布时间】:2020-09-19 16:59:48
【问题描述】:
我想要一个强类型代码。当我应用类似问题的解决方案时 - Min/Max of dates in an array? - 我收到错误
TS2345: Argument of type 'Date' is not assignable to parameter of type 'number'.
我的代码
const min: Date = Math.min(begin as Date, (end || defaultDate) as Date);
const max: Date = Math.max(begin as Date, (end || defaultDate) as Date);
begin as Date 部分带有下划线。
在 Typescript 中查找最小/最大日期的正确方法是什么?
【问题讨论】:
-
Math.min()和Math.max()返回一个数字。您不能让它们返回带有类型的 Date 对象。 -
@GuyIncognito 我明白了,我明白了。但正确的方法是什么?
const min: Date = new Date(Math.min(begin.getDate(), (end || hover).getDate()))?
标签: typescript date max min