如何在 Typescript 中正确导入 MomentJS 以解决以下错误:“Type 'typeof moment' has no compatible call signatures”。

Lin*_*son 1 javascript momentjs typescript angular

导入 MomentJS 导致以下错误:

ERROR in [...] Cannot invoke an expression whose type lacks a call signature. Type 'typeof moment' has no compatible call signatures.
Run Code Online (Sandbox Code Playgroud)

我该如何解决这个错误?

Lin*_*son 9

最新版本的 MomentJS 现在已经定义了类型。在没有外部类型的情况下重新安装 MomentJS。您还应该避免名称空间样式的导入,因为此类导入无法调用或构造,并且会导致运行时失败。

要走的路是传统的进口:

import moment from 'moment';

  • @vidalsasoom 那又怎样?自我回答是好事 (10认同)