【问题标题】:How resolve Error:(4, 1) TS2349: Cannot invoke an expression whose type lacks a call signature. Type 'typeof moment' has no compatible call signatures如何解决错误:(4, 1) TS2349:无法调用其类型缺少调用签名的表达式。类型“typeof moment”没有兼容的调用签名
【发布时间】:2020-01-29 12:24:20
【问题描述】:

使用 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'))

【问题讨论】:

    标签: typescript


    【解决方案1】:

    我没有你的 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

    【讨论】:

      猜你喜欢
      • 2017-10-11
      • 2020-05-17
      • 2019-10-08
      • 1970-01-01
      • 2023-03-20
      • 1970-01-01
      • 2020-02-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多