programing

Typescript + Express: 'type of e' 유형에 호환되는 호출 서명이 없습니다.

iphone6s 2023. 7. 1. 08:09
반응형

Typescript + Express: 'type of e' 유형에 호환되는 호출 서명이 없습니다.

typescript, express를 사용하여 응용 프로그램을 빌드하려고 하는데 다음 오류가 발생합니다.Cannot invoke an expression whose type lacks a call signature. Type 'typeof e' has no compatible call signatures(app.ts에서 express ()을 호출합니다.

저는 제 개발을 돕기 위해 여기 웹팩을 사용하고 있습니다.

내 패키지.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그리고 내 빌드는 명시된 오류와 함께 실패합니다. 몇 개의 블로그에서 해결책을 찾으려 했지만 아무도 이 오류에 대해 언급하지 않았습니다.나는 간신히 추가했습니다.express.Application의 타입으로서app안에서.app.ts내가 뭘 잘못하고 있는 거지?웹팩의 구성 때문입니까?

어떤 도움이든 감사합니다.

네임스페이스 대신 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();

저는 수입품을 바꾸는데 효과가 있었습니다.

import { express } from "express";

const app = express();

언급URL : https://stackoverflow.com/questions/53142239/typescript-express-type-typeof-e-has-no-compatible-call-signatures

반응형