gpt4 book ai didi

node.js - typescript + Express : Type 'typeof e' has no compatible call signatures

转载 作者:行者123 更新时间:2023-12-03 12:09:42 32 4
gpt4 key购买 nike

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

我在这里使用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,并且构建失败并显示错误。
尝试在一些博客中寻找解决方案,但都没有提到有关此错误的tanything。我设法将 express.Application作为 app的类型添加到一侧 app.ts
我究竟做错了什么 ?是否由于webpack的配置?

任何帮助表示赞赏

最佳答案

您需要从express而不是 namespace (这是具有所有已命名导出的对象)中导入默认导出。

在您的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();

关于node.js - typescript + Express : Type 'typeof e' has no compatible call signatures,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53142239/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com