如何解决错误:(4, 1) TS2349:无法调用类型缺少调用签名的表达式。类型“typeof moment”没有兼容的调用签名

MMMI岛

使用 Typescript 3.* 和 momentjs 作为日期,我遇到了编译时错误。

错误:(3, 13) TS2349:无法调用类型缺少调用签名的表达式。类型“typeof moment”没有兼容的调用签名。

所以,我寻找解决方案来解决这个错误。并找到下面的文章,我做了与文章中描述的解决方案相同的事情。

但我再次遇到编译时错误。

错误:(1, 8) TS1192:模块“/Users/hogehoge/Work/Work_Fork/hoge/node_modules/moment/moment”没有默认导出。

这是我的 tsconfig 和我的程序。

{
  "compilerOptions": {
    "moduleResolution": "node",
    "skipLibCheck": false,
    "target": "es5",
    "module": "commonjs",
    "lib": ["es2017", "dom"],
    "experimentalDecorators": true,
    "allowJs": false,
    "jsx": "react",
    "sourceMap": true,
    "strict": true,
    "noImplicitReturns": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "noFallthroughCasesInSwitch": true,
    "esModuleInterop": true,
    "removeComments": true,
    "newLine": "LF",
    "downlevelIteration": true,
    "resolveJsonModule": true,
    "allowSyntheticDefaultImports": false
  },
  "exclude": ["node_modules"]
}

import moment from 'moment';

console.log(moment().format('YYYY-MM-DD'))
盖克杰夫

我没有你的 package.json,所以我以这种方式重现了你的情况:

npm init
npm i typescript moment @types/node

文件 test.ts

let moment = require("moment");
console.log(moment().format('YYYY-MM-DD'))

(使用require代替import

然后执行“编译器”:

_modules/.bin/tsc test.ts

没有错误,这样对我来说效果很好。截至今天,该脚本返回2019-09-30.

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章