Typescript + Express:类型“typeof e”没有兼容的调用签名

Posted

技术标签:

【中文标题】Typescript + Express:类型“typeof e”没有兼容的调用签名【英文标题】:Typescript + Express : Type 'typeof e' has no compatible call signatures 【发布时间】:2019-04-08 02:17:07 【问题描述】:

我正在尝试使用 typescript 、 express 构建应用程序。但我收到了这个错误: Cannot invoke an expression whose type lacks a call signature. Type 'typeof e' has no compatible call signatures(在 app.ts 中调用 express() )

我在这里使用 webpack 来帮助我的开发。

我的 Package.json :

"scripts" :
    "build": "webpack" 
 ,
 "dependencies": 
    "body-parser": "^1.18.3",
    "dotenv": "^6.1.0",
    "jsonwebtoken": "^8.3.0",
    "nodemon": "^1.18.5"
  ,
  "devDependencies": 
    "@types/body-parser": "^1.17.0",
    "@types/dotenv": "^4.0.3",
    "@types/express": "^4.16.0",
    "clean-webpack-plugin": "^0.1.19",
    "ts-loader": "^5.3.0",
    "ts-node": "^7.0.1",
    "typescript": "^3.1.6",
    "webpack": "^4.24.0",
    "webpack-cli": "^3.1.2"
  

我的webpack.confg.js

var path = require("path");
const CleanWebpackPlugin = require("clean-webpack-plugin");

var fs = require("fs");
var nodeModules = ;
fs.readdirSync("node_modules")
  .filter(function(x) 
    return [".bin"].indexOf(x) === -1;
  )
  .forEach(function(mod) 
    nodeModules[mod] = "commonjs " + mod;
  );

module.exports = 
  entry: "./src/index.ts",

  plugins: [new CleanWebpackPlugin(["./dist"])],
  output: 
    filename: "index.js",
    path: path.resolve(__dirname, "dist")
  ,
  module: 
    rules: [
      //all files with .ts extention will be handled y ts-loader
       test: /\.ts$/, loader: "ts-loader" 
    ]
  ,
  target: "node",
  externals: nodeModules
;

我的app.ts

import * as express from "express";
import * as bodyParser from "body-parser";

class App 
  public app: express.Application;
  constructor() 
    this.app = express();
    this.config();
  

  private config(): void 
    //add support for application/json type for data
    this.app.use(bodyParser.json());

    //support application/x-www-form-urlencoded post data
    this.app.use(bodyParser.urlencoded( extended: false ));
  


export default new App().app;

我正在运行npm run build,但我的构建失败并出现错误。 尝试在一些博客中搜索解决方案,但没有人提到有关此错误的任何信息。我​​设法在app.ts 中添加express.Application 作为app 的类型 我究竟做错了什么 ?是不是因为webpack的配置?

任何帮助表示赞赏

【问题讨论】:

您的依赖项中是否缺少express @pzaenger 是的,我想我错过了。但它仍然是一样的:(。添加快递后错误仍然存​​在 @ZeroCho 它在我打电话给express() 的同一个地方。我想我解决了。刚刚将导入更改为:import express from "express"。它解决了这个问题。 @pzaenger,感谢您为我指明正确的方向 @pzaenger 为什么import express from "express" 有效而import * as express from "express" 无效? 在这个答案中,你可以得到esModuleInterop是如何导致这个错误的详细解释:***.com/questions/56238356/… 【参考方案1】:

您需要从 express 而不是命名空间(这是一个包含所有命名导出的对象)导入默认导出。

在您的 app.ts 中,这应该就是您所需要的:

// Change these
import express from "express";
import bodyParser from "body-parser";

区别在于:

// Namespace import
import * as express from "express";

const app = express.default();

// Default import
import express from "express";

const app = express();

【讨论】:

我得到这个:“typeof e”类型上不存在属性“default”。【参考方案2】:

对我来说,它有助于更​​改导入方式-

import  express  from "express";

const app = express();

【讨论】:

以上是关于Typescript + Express:类型“typeof e”没有兼容的调用签名的主要内容,如果未能解决你的问题,请参考以下文章