【发布时间】:2021-12-06 22:56:17
【问题描述】:
我正在尝试使用 new Date() 获取当前时间:
function getTime(): Date{
const d = new Date()
return d
}
但这给出了错误:
'new' expression, whose target lacks a construct signature, implicitly has an 'any' type.
将new Date() 与打字稿一起使用的正确方法是什么?
【问题讨论】:
-
这是正确的方法。您是否在同一文件的某处导入或覆盖了
Date? -
日期有构造函数;事实上,Date 被定义为type DateConstructor。你的代码在我看来是正确的,it works on the Playground。也许您的 Date 导入或类与内置的冲突?
标签: typescript