带有 Express 的 TypeScript:类型 'typeof import("express")' 没有调用签名

Sha*_*oon 9 express typescript

我的错误是:

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

我的tsconfig.json是:

{
    "compilerOptions": {
        "outDir": "./built",
        "allowJs": true,
        "target": "es6",
        "esModuleInterop": true
    },
    "include": [
        "./src/**/*"
    ]
}
Run Code Online (Sandbox Code Playgroud)

我的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()
Run Code Online (Sandbox Code Playgroud)

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

我究竟做错了什么?

小智 25

要使此功能"esModuleInterop": true在 tsconfig.json 中设置为 true,您也可以执行此操作。

import * as express from 'express';
...
const app = express.default();
Run Code Online (Sandbox Code Playgroud)

来源


小智 23

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

  • 阅读本文以了解为什么您不应该**从 tsconfig.json 中删除“esModuleInterop”:/sf/answers/3944370251/(如果您使用的是 TS 版本 2.7+) (2认同)

win*_*iz1 8

添加@types/express然后:

import * as express from "express";
...
const app = express();
Run Code Online (Sandbox Code Playgroud)