【问题标题】:TypeScript with Express: Type 'typeof import("express")' has no call signatures带有 Express 的 TypeScript:类型 'typeof import("express")' 没有调用签名
【发布时间】:2020-01-22 07:13:15
【问题描述】:

我的错误是:

Error: src/app.ts(11,13): error TS2349: This expression is not callable.
  Type 'typeof import("express")' has no call signatures.

我的tsconfig.json 是:

{
    "compilerOptions": {
        "outDir": "./built",
        "allowJs": true,
        "target": "es6",
        "esModuleInterop": true
    },
    "include": [
        "./src/**/*"
    ]
}

我的src/app.ts 有:

// const Logger = require('./lib/logger')
import express from 'express';
import bodyParser from 'body-parser';
// const finale = require('finale-rest')
// const morgan = require('morgan')
const DB = require('./models')()


// const resources = require('./resources')

const app = express()

有问题的线路是const app = express()

我做错了什么?

【问题讨论】:

  • 我希望这可以工作,在我的机器上尝试过(安装了@types/express)并且你的代码可以工作..
  • TS 2.7+ 的正确解决方案:stackoverflow.com/a/56348146/2678608

标签: typescript express


【解决方案1】:

确保您没有在 tsconfig.json 中设置 "esModuleInterop": true。禁用此设置为我解决了这个问题。

【讨论】:

  • 对我来说,winwiz1 的解决方案已经到位,但我仍然收到错误消息。最重要的是,按照 Nathan Hopper 的建议为我解决了这个问题。
  • 阅读本文以了解为什么您应该从 tsconfig.json 中删除“esModuleInterop”:stackoverflow.com/a/56348146/2678608(如果您使用的是 TS 版本 2.7+)
  • 是的,但是如果您有像我这样需要互操作设置的引用怎么办?
【解决方案2】:

添加@types/express,然后:

import * as express from "express";
...
const app = express();

【讨论】:

    【解决方案3】:

    若要在 tsconfig.json 中将 "esModuleInterop": true 设置为 true,您也可以这样做。

    import * as express from 'express';
    ...
    const app = express.default();
    

    source

    【讨论】:

      猜你喜欢
      • 2019-04-08
      • 2019-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-09
      • 2019-04-06
      • 2020-09-06
      • 2020-10-17
      相关资源
      最近更新 更多