我想要一个强类型代码。当我使用类似问题的解决方案时-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
部分带有下划线。
在打字稿中查找最小/最大日期的正确方法是什么?
您可以在打字稿中像这样比较日期:
const begin: Date = new Date();
const end: Date = new Date();
const min: Date = begin < end ? begin : end;
const max: Date = begin > end ? begin : end;
您遇到的问题是Math.min
返回一个数字。